Skip to content

Commit 28bd095

Browse files
SciresMWinterMute
authored andcommitted
elf2nro: add support for aligned header flag
1 parent e36a825 commit 28bd095

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/elf2nro.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ typedef struct {
2121

2222
typedef struct {
2323
u8 Magic[4];
24-
u32 Unk1;
24+
u32 version;
2525
u32 size;
26-
u32 Unk2;
26+
u32 flags;
2727
NsoSegment Segments[3];
2828
u32 bssSize;
2929
u32 Unk3;
@@ -78,9 +78,10 @@ int main(int argc, char* argv[]) {
7878
fprintf(stderr, "--nacp=<control.nacp> Embeds control.nacp into the output file.\n");
7979
fprintf(stderr, "--romfs=<image> Embeds RomFS into the output file.\n");
8080
fprintf(stderr, "--romfsdir=<directory> Builds and embeds RomFS into the output file.\n");
81+
fprintf(stderr, "--alignedheader Sets the \"AlignedHeader\" flag in the output file.\n");
8182
return EXIT_FAILURE;
8283
}
83-
84+
8485
NroStart nro_start;
8586
memset(&nro_start, 0, sizeof(nro_start));
8687

@@ -102,18 +103,20 @@ int main(int argc, char* argv[]) {
102103

103104
int argi;
104105
char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL;
106+
u32 aligned_header = 0;
105107
for (argi=3; argi<argc; argi++) {
106108
if (strncmp(argv[argi], "--icon=", 7)==0) icon_path = &argv[argi][7];
107109
if (strncmp(argv[argi], "--nacp=", 7)==0) nacp_path = &argv[argi][7];
108110
if (strncmp(argv[argi], "--romfs=", 8)==0) romfs_path = &argv[argi][8];
109111
if (strncmp(argv[argi], "--romfsdir=", 11)==0) romfs_dir_path = &argv[argi][11];
112+
if (strncmp(argv[argi], "--alignedheader", 15)==0) aligned_header = 1;
110113
}
111-
114+
112115
if (romfs_dir_path != NULL && romfs_path != NULL) {
113116
fprintf(stderr, "Cannot have a RomFS and a RomFS Directory at the same time!\n");
114117
return EXIT_FAILURE;
115118
}
116-
119+
117120
if (elf_len < sizeof(Elf64_Ehdr)) {
118121
fprintf(stderr, "Input file doesn't fit ELF header!\n");
119122
return EXIT_FAILURE;
@@ -154,7 +157,7 @@ int main(int argc, char* argv[]) {
154157
fprintf(stderr, "Invalid ELF: expected 3 loadable phdrs and a bss!\n");
155158
return EXIT_FAILURE;
156159
}
157-
160+
158161
// .bss is special
159162
if (i == 3) {
160163
tmpsize = (phdr->p_filesz + 0xFFF) & ~0xFFF;
@@ -174,13 +177,13 @@ int main(int argc, char* argv[]) {
174177
fprintf(stderr, "Out of memory!\n");
175178
return EXIT_FAILURE;
176179
}
177-
180+
178181
memcpy(buf[i], &elf[phdr->p_offset], phdr->p_filesz);
179182

180183
file_off += nro_hdr.Segments[i].Size;
181184
file_off = (file_off + 0xFFF) & ~0xFFF;
182185
}
183-
186+
184187
/* Iterate over sections to find build id. */
185188
size_t cur_sect_hdr_ofs = hdr->e_shoff;
186189
for (unsigned int i = 0; i < hdr->e_shnum; i++) {
@@ -206,9 +209,12 @@ int main(int argc, char* argv[]) {
206209
fprintf(stderr, "Failed to open output file!\n");
207210
return EXIT_FAILURE;
208211
}
209-
212+
210213
nro_hdr.size = file_off;
211214

215+
nro_hdr.version = 0;
216+
nro_hdr.flags = (aligned_header << 0);
217+
212218
// TODO check retvals
213219

214220
for (i=0; i<3; i++)
@@ -270,7 +276,7 @@ int main(int argc, char* argv[]) {
270276
asset_hdr.romfs.offset = tmp_off;
271277
asset_hdr.romfs.size = romfs_len;
272278
tmp_off+= romfs_len;
273-
279+
274280
} else if (romfs_dir_path) {
275281
asset_hdr.romfs.offset = tmp_off;
276282
asset_hdr.romfs.size = build_romfs_by_path_into_file(romfs_dir_path, out, file_off + tmp_off);

0 commit comments

Comments
 (0)