From a9c7fe86260ca906c7378503e059eca0ad014947 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 24 Jan 2023 22:59:59 +0900 Subject: [PATCH] bootctl-uki: several coding style fixlets Mostly follow-ups for #26082. (cherry picked from commit 5b532e14e3ac1d8f8cef90fd8c5b1ce277458ae7) Related: RHEL-16354 --- src/boot/bootctl-uki.c | 105 +++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 45 deletions(-) diff --git a/src/boot/bootctl-uki.c b/src/boot/bootctl-uki.c index 6bdf926350..3492fa548f 100644 --- a/src/boot/bootctl-uki.c +++ b/src/boot/bootctl-uki.c @@ -23,7 +23,10 @@ static int pe_sections(FILE *uki, struct PeSectionHeader **ret, size_t *ret_n) { struct PeHeader pe; size_t scount; uint64_t soff, items; - int rc; + + assert(uki); + assert(ret); + assert(ret_n); items = fread(&dos, 1, sizeof(dos), uki); if (items != sizeof(dos)) @@ -31,9 +34,9 @@ static int pe_sections(FILE *uki, struct PeSectionHeader **ret, size_t *ret_n) { if (memcmp(dos.Magic, dos_file_magic, sizeof(dos_file_magic)) != 0) goto no_sections; - rc = fseek(uki, le32toh(dos.ExeHeader), SEEK_SET); - if (rc < 0) + if (fseek(uki, le32toh(dos.ExeHeader), SEEK_SET) < 0) return log_error_errno(errno, "seek to PE header"); + items = fread(&pe, 1, sizeof(pe), uki); if (items != sizeof(pe)) return log_error_errno(SYNTHETIC_ERRNO(EIO), "PE header read error"); @@ -41,8 +44,7 @@ static int pe_sections(FILE *uki, struct PeSectionHeader **ret, size_t *ret_n) { goto no_sections; soff = le32toh(dos.ExeHeader) + sizeof(pe) + le16toh(pe.FileHeader.SizeOfOptionalHeader); - rc = fseek(uki, soff, SEEK_SET); - if (rc < 0) + if (fseek(uki, soff, SEEK_SET) < 0) return log_error_errno(errno, "seek to PE section headers"); scount = le16toh(pe.FileHeader.NumberOfSections); @@ -65,58 +67,72 @@ no_sections: return 0; } -static int find_pe_section(struct PeSectionHeader *sections, size_t scount, - const uint8_t *name, size_t namelen, size_t *ret) { - for (size_t s = 0; s < scount; s++) { - if (memcmp_nn(sections[s].Name, sizeof(sections[s].Name), - name, namelen) == 0) { +static bool find_pe_section( + struct PeSectionHeader *sections, + size_t scount, + const uint8_t *name, + size_t namelen, + size_t *ret) { + + assert(sections || scount == 0); + assert(name || namelen == 0); + + for (size_t s = 0; s < scount; s++) + if (memcmp_nn(sections[s].Name, sizeof(sections[s].Name), name, namelen) == 0) { if (ret) *ret = s; - return 1; + return true; } - } - return 0; + + return false; } static bool is_uki(struct PeSectionHeader *sections, size_t scount) { - return (find_pe_section(sections, scount, name_osrel, sizeof(name_osrel), NULL) && + assert(sections || scount == 0); + + return + find_pe_section(sections, scount, name_osrel, sizeof(name_osrel), NULL) && find_pe_section(sections, scount, name_linux, sizeof(name_linux), NULL) && - find_pe_section(sections, scount, name_initrd, sizeof(name_initrd), NULL)); + find_pe_section(sections, scount, name_initrd, sizeof(name_initrd), NULL); } int verb_kernel_identify(int argc, char *argv[], void *userdata) { _cleanup_fclose_ FILE *uki = NULL; _cleanup_free_ struct PeSectionHeader *sections = NULL; size_t scount; - int rc; + int r; uki = fopen(argv[1], "re"); if (!uki) return log_error_errno(errno, "Failed to open UKI file '%s': %m", argv[1]); - rc = pe_sections(uki, §ions, &scount); - if (rc < 0) - return EXIT_FAILURE; + r = pe_sections(uki, §ions, &scount); + if (r < 0) + return r; - if (sections) { - if (is_uki(sections, scount)) { - puts("uki"); - return EXIT_SUCCESS; - } + if (!sections) + puts("unknown"); + else if (is_uki(sections, scount)) + puts("uki"); + else puts("pe"); - return EXIT_SUCCESS; - } - puts("unknown"); return EXIT_SUCCESS; } -static int read_pe_section(FILE *uki, const struct PeSectionHeader *section, - void **ret, size_t *ret_n) { +static int read_pe_section( + FILE *uki, + const struct PeSectionHeader *section, + void **ret, + size_t *ret_n) { + _cleanup_free_ void *data = NULL; uint32_t size, bytes; uint64_t soff; - int rc; + + assert(uki); + assert(section); + assert(ret); soff = le32toh(section->PointerToRawData); size = le32toh(section->VirtualSize); @@ -124,8 +140,7 @@ static int read_pe_section(FILE *uki, const struct PeSectionHeader *section, if (size > 16 * 1024) return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "PE section too big"); - rc = fseek(uki, soff, SEEK_SET); - if (rc < 0) + if (fseek(uki, soff, SEEK_SET) < 0) return log_error_errno(errno, "seek to PE section"); data = malloc(size+1); @@ -169,6 +184,9 @@ static void inspect_uki(FILE *uki, struct PeSectionHeader *sections, size_t scou _cleanup_free_ char *osrel = NULL; size_t osrel_size, idx; + assert(uki); + assert(sections || scount == 0); + if (find_pe_section(sections, scount, name_cmdline, sizeof(name_cmdline), &idx)) read_pe_section(uki, sections + idx, (void**)&cmdline, NULL); @@ -190,26 +208,23 @@ int verb_kernel_inspect(int argc, char *argv[], void *userdata) { _cleanup_fclose_ FILE *uki = NULL; _cleanup_free_ struct PeSectionHeader *sections = NULL; size_t scount; - int rc; + int r; uki = fopen(argv[1], "re"); if (!uki) return log_error_errno(errno, "Failed to open UKI file '%s': %m", argv[1]); - rc = pe_sections(uki, §ions, &scount); - if (rc < 0) - return EXIT_FAILURE; + r = pe_sections(uki, §ions, &scount); + if (r < 0) + return r; - if (sections) { - if (is_uki(sections, scount)) { - puts("Kernel Type: uki"); - inspect_uki(uki, sections, scount); - return EXIT_SUCCESS; - } + if (!sections) + puts("Kernel Type: unknown"); + else if (is_uki(sections, scount)) { + puts("Kernel Type: uki"); + inspect_uki(uki, sections, scount); + } else puts("Kernel Type: pe"); - return EXIT_SUCCESS; - } - puts("Kernel Type: unknown"); return EXIT_SUCCESS; }