Skip to content

Commit f97d461

Browse files
SudhakarKuppusamy1Daniel Kiper
authored and
Daniel Kiper
committed
grub-mkimage: Create new ELF note for SBAT
In order to store the SBAT data we create a new ELF note. The string ".sbat", zero-padded to 4 byte alignment, shall be entered in the name field. The string "SBAT"'s ASCII values, 0x53424154, should be entered in the type field. Signed-off-by: Daniel Axtens <[email protected]> Signed-off-by: Sudhakar Kuppusamy <[email protected]> Reviewed-by: Daniel Kiper <[email protected]>
1 parent f26b398 commit f97d461

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

include/grub/util/mkimage.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ grub_mkimage_load_image64 (const char *kernel_path,
5151
const struct grub_install_image_target_desc *image_target);
5252
void
5353
grub_mkimage_generate_elf32 (const struct grub_install_image_target_desc *image_target,
54-
int note, char **core_img, size_t *core_size,
54+
int note, char *sbat, char **core_img, size_t *core_size,
5555
Elf32_Addr target_addr,
5656
struct grub_mkimage_layout *layout);
5757
void
5858
grub_mkimage_generate_elf64 (const struct grub_install_image_target_desc *image_target,
59-
int note, char **core_img, size_t *core_size,
59+
int note, char *sbat, char **core_img, size_t *core_size,
6060
Elf64_Addr target_addr,
6161
struct grub_mkimage_layout *layout);
6262

util/grub-mkimagexx.c

+46-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ struct section_metadata
107107
const char *strtab;
108108
};
109109

110+
#define GRUB_SBAT_NOTE_NAME ".sbat"
111+
#define GRUB_SBAT_NOTE_TYPE 0x53424154 /* "SBAT" */
112+
113+
struct grub_sbat_note {
114+
Elf32_Nhdr header;
115+
char name[ALIGN_UP(sizeof(GRUB_SBAT_NOTE_NAME), 4)];
116+
};
117+
110118
static int
111119
is_relocatable (const struct grub_install_image_target_desc *image_target)
112120
{
@@ -208,7 +216,7 @@ grub_arm_reloc_jump24 (grub_uint32_t *target, Elf32_Addr sym_addr)
208216

209217
void
210218
SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc *image_target,
211-
int note, char **core_img, size_t *core_size,
219+
int note, char *sbat, char **core_img, size_t *core_size,
212220
Elf_Addr target_addr,
213221
struct grub_mkimage_layout *layout)
214222
{
@@ -217,10 +225,17 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
217225
Elf_Ehdr *ehdr;
218226
Elf_Phdr *phdr;
219227
Elf_Shdr *shdr;
220-
int header_size, footer_size = 0;
228+
int header_size, footer_size = 0, footer_offset = 0;
221229
int phnum = 1;
222230
int shnum = 4;
223231
int string_size = sizeof (".text") + sizeof ("mods") + 1;
232+
char *footer;
233+
234+
if (sbat)
235+
{
236+
phnum++;
237+
footer_size += ALIGN_UP (sizeof (struct grub_sbat_note) + layout->sbat_size, 4);
238+
}
224239

225240
if (image_target->id != IMAGE_LOONGSON_ELF)
226241
phnum += 2;
@@ -248,6 +263,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
248263
ehdr = (void *) elf_img;
249264
phdr = (void *) (elf_img + sizeof (*ehdr));
250265
shdr = (void *) (elf_img + sizeof (*ehdr) + phnum * sizeof (*phdr));
266+
footer = elf_img + program_size + header_size;
251267
memcpy (ehdr->e_ident, ELFMAG, SELFMAG);
252268
ehdr->e_ident[EI_CLASS] = ELFCLASSXX;
253269
if (!image_target->bigendian)
@@ -420,6 +436,8 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
420436
phdr->p_filesz = grub_host_to_target32 (XEN_NOTE_SIZE);
421437
phdr->p_memsz = 0;
422438
phdr->p_offset = grub_host_to_target32 (header_size + program_size);
439+
footer = ptr;
440+
footer_offset = XEN_NOTE_SIZE;
423441
}
424442

425443
if (image_target->id == IMAGE_XEN_PVH)
@@ -453,6 +471,8 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
453471
phdr->p_filesz = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
454472
phdr->p_memsz = 0;
455473
phdr->p_offset = grub_host_to_target32 (header_size + program_size);
474+
footer = ptr;
475+
footer_offset = XEN_PVH_NOTE_SIZE;
456476
}
457477

458478
if (note)
@@ -483,6 +503,30 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
483503
phdr->p_filesz = grub_host_to_target32 (note_size);
484504
phdr->p_memsz = 0;
485505
phdr->p_offset = grub_host_to_target32 (header_size + program_size);
506+
footer = (elf_img + program_size + header_size + note_size);
507+
footer_offset += note_size;
508+
}
509+
510+
if (sbat)
511+
{
512+
int note_size = ALIGN_UP (sizeof (struct grub_sbat_note) + layout->sbat_size, 4);
513+
struct grub_sbat_note *note_ptr = (struct grub_sbat_note *) footer;
514+
515+
note_ptr->header.n_namesz = grub_host_to_target32 (sizeof (GRUB_SBAT_NOTE_NAME));
516+
note_ptr->header.n_descsz = grub_host_to_target32 (ALIGN_UP(layout->sbat_size, 4));
517+
note_ptr->header.n_type = grub_host_to_target32 (GRUB_SBAT_NOTE_TYPE);
518+
memcpy (note_ptr->name, GRUB_SBAT_NOTE_NAME, sizeof (GRUB_SBAT_NOTE_NAME));
519+
memcpy ((char *)(note_ptr + 1), sbat, layout->sbat_size);
520+
521+
phdr++;
522+
phdr->p_type = grub_host_to_target32 (PT_NOTE);
523+
phdr->p_flags = grub_host_to_target32 (PF_R);
524+
phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
525+
phdr->p_vaddr = 0;
526+
phdr->p_paddr = 0;
527+
phdr->p_filesz = grub_host_to_target32 (note_size);
528+
phdr->p_memsz = 0;
529+
phdr->p_offset = grub_host_to_target32 (header_size + program_size + footer_offset);
486530
}
487531

488532
{

util/mkimage.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
18141814
case IMAGE_I386_IEEE1275:
18151815
{
18161816
grub_uint64_t target_addr;
1817+
char *sbat = NULL;
18171818
if (image_target->id == IMAGE_LOONGSON_ELF)
18181819
{
18191820
if (comp == GRUB_COMPRESSION_NONE)
@@ -1825,10 +1826,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
18251826
else
18261827
target_addr = image_target->link_addr;
18271828
if (image_target->voidp_sizeof == 4)
1828-
grub_mkimage_generate_elf32 (image_target, note, &core_img, &core_size,
1829+
grub_mkimage_generate_elf32 (image_target, note, sbat, &core_img, &core_size,
18291830
target_addr, &layout);
18301831
else
1831-
grub_mkimage_generate_elf64 (image_target, note, &core_img, &core_size,
1832+
grub_mkimage_generate_elf64 (image_target, note, sbat, &core_img, &core_size,
18321833
target_addr, &layout);
18331834
}
18341835
break;

0 commit comments

Comments
 (0)