Skip to content

Commit

Permalink
x86, tools: Consolidate #ifdef code
Browse files Browse the repository at this point in the history
Instead of littering main() with #ifdef CONFIG_EFI_STUB, move the logic
into separate functions that do nothing if the config option isn't set.
This makes main() much easier to read.

Acked-by: Borislav Petkov <bp@suse.de>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
  • Loading branch information
Matt Fleming committed Mar 4, 2014
1 parent 86134a1 commit 993c30a
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions arch/x86/boot/tools/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,45 @@ static void update_pecoff_text(unsigned int text_start, unsigned int file_sz)
update_pecoff_section_header(".text", text_start, text_sz);
}

static int reserve_pecoff_reloc_section(int c)
{
/* Reserve 0x20 bytes for .reloc section */
memset(buf+c, 0, PECOFF_RELOC_RESERVE);
return PECOFF_RELOC_RESERVE;
}

static void efi_stub_defaults(void)
{
/* Defaults for old kernel */
#ifdef CONFIG_X86_32
efi_pe_entry = 0x10;
efi_stub_entry = 0x30;
#else
efi_pe_entry = 0x210;
efi_stub_entry = 0x230;
startup_64 = 0x200;
#endif
}

static void efi_stub_entry_update(void)
{
#ifdef CONFIG_X86_64 /* Yes, this is really how we defined it :( */
efi_stub_entry -= 0x200;
#endif
put_unaligned_le32(efi_stub_entry, &buf[0x264]);
}

#else

static inline void update_pecoff_setup_and_reloc(unsigned int) {}
static inline void update_pecoff_text(unsigned int, unsigned int) {}
static inline void efi_stub_defaults(void) {}
static inline void efi_stup_entry_update(void) {}

static inline int reserve_pecoff_reloc_section(int c)
{
return 0;
}
#endif /* CONFIG_EFI_STUB */


Expand Down Expand Up @@ -271,15 +310,7 @@ int main(int argc, char ** argv)
void *kernel;
u32 crc = 0xffffffffUL;

/* Defaults for old kernel */
#ifdef CONFIG_X86_32
efi_pe_entry = 0x10;
efi_stub_entry = 0x30;
#else
efi_pe_entry = 0x210;
efi_stub_entry = 0x230;
startup_64 = 0x200;
#endif
efi_stub_defaults();

if (argc != 5)
usage();
Expand All @@ -302,11 +333,7 @@ int main(int argc, char ** argv)
die("Boot block hasn't got boot flag (0xAA55)");
fclose(file);

#ifdef CONFIG_EFI_STUB
/* Reserve 0x20 bytes for .reloc section */
memset(buf+c, 0, PECOFF_RELOC_RESERVE);
c += PECOFF_RELOC_RESERVE;
#endif
c += reserve_pecoff_reloc_section(c);

/* Pad unused space with zeros */
setup_sectors = (c + 511) / 512;
Expand All @@ -315,9 +342,7 @@ int main(int argc, char ** argv)
i = setup_sectors*512;
memset(buf+c, 0, i-c);

#ifdef CONFIG_EFI_STUB
update_pecoff_setup_and_reloc(i);
#endif

/* Set the default root device */
put_unaligned_le16(DEFAULT_ROOT_DEV, &buf[508]);
Expand All @@ -342,14 +367,9 @@ int main(int argc, char ** argv)
buf[0x1f1] = setup_sectors-1;
put_unaligned_le32(sys_size, &buf[0x1f4]);

#ifdef CONFIG_EFI_STUB
update_pecoff_text(setup_sectors * 512, sz + i + ((sys_size * 16) - sz));

#ifdef CONFIG_X86_64 /* Yes, this is really how we defined it :( */
efi_stub_entry -= 0x200;
#endif
put_unaligned_le32(efi_stub_entry, &buf[0x264]);
#endif
efi_stub_entry_update();

crc = partial_crc32(buf, i, crc);
if (fwrite(buf, 1, i, dest) != i)
Expand Down

0 comments on commit 993c30a

Please sign in to comment.