flashrom: Forgot some things in r3899.
[coreboot.git] / util / mkelfImage / linux-i386 / mkelf-linux-i386.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <string.h>
6 #define _GNU_SOURCE
7 #include <getopt.h>
8 #include "elf.h"
9 #include "elf_boot.h"
10 #include "convert.h"
11 #include "x86-linux.h"
12 #include "mkelfImage.h"
13
14 static unsigned char payload[] = {
15 #include "convert.bin.c"
16 };
17
18 struct kernel_info;
19 static void (*parse_kernel_type)(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
20 static void parse_bzImage_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
21 static void parse_elf32_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
22 static void parse_elf64_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
23
24 char *vmlinux_x86_64_probe(char *kernel_buf, off_t kernel_size);
25
26 char *vmlinux_i386_probe(char *kernel_buf, off_t kernel_size)
27 {
28         Elf32_Ehdr *ehdr;
29         Elf32_Phdr *phdr;
30         int i;
31         int phdrs;
32         ehdr = (Elf32_Ehdr *)kernel_buf;
33         if (
34                 (ehdr->e_ident[EI_MAG0] != ELFMAG0) ||
35                 (ehdr->e_ident[EI_MAG1] != ELFMAG1) ||
36                 (ehdr->e_ident[EI_MAG2] != ELFMAG2) ||
37                 (ehdr->e_ident[EI_MAG3] != ELFMAG3)) {
38                 return "No ELF signature found on kernel\n";
39         }
40         if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
41                 return vmlinux_x86_64_probe(kernel_buf, kernel_size);
42 //              return "Not a 32bit ELF kernel\n";
43         }
44         if (ehdr->e_ident[EI_DATA]  != ELFDATA2LSB) {
45                 return "Not a little endian ELF kernel\n";
46         }
47         if (le16_to_cpu(ehdr->e_type) != ET_EXEC) {
48                 return "Not an executable kernel\n";
49         }
50         if (le16_to_cpu(ehdr->e_machine) != EM_386) {
51                 return "Not an i386 kernel\n";
52         }
53         if (    (ehdr->e_ident[EI_VERSION] != EV_CURRENT) ||
54                 (le32_to_cpu(ehdr->e_version) != EV_CURRENT)) {
55                 return "Kernel not using ELF version 1.\n";
56         }
57         if (le16_to_cpu(ehdr->e_phentsize) != sizeof(*phdr)) {
58                 return "Kernel uses bad program header size.\n";
59         }
60         phdr = (Elf32_Phdr *)(kernel_buf + le32_to_cpu(ehdr->e_phoff));
61         phdrs = 0;
62         for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
63                 if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
64                         continue;
65                 phdrs++;
66         }
67         if (phdrs == 0) {
68                 return "No PT_LOAD segments!\n";
69         }
70         parse_kernel_type = parse_elf32_kernel;
71         return 0;
72 }
73 char *vmlinux_x86_64_probe(char *kernel_buf, off_t kernel_size)
74 {
75         Elf64_Ehdr *ehdr;
76         Elf64_Phdr *phdr;
77         int i;
78         int phdrs = 0;
79         ehdr = (Elf64_Ehdr *)kernel_buf;
80         if (
81                 (ehdr->e_ident[EI_MAG0] != ELFMAG0) ||
82                 (ehdr->e_ident[EI_MAG1] != ELFMAG1) ||
83                 (ehdr->e_ident[EI_MAG2] != ELFMAG2) ||
84                 (ehdr->e_ident[EI_MAG3] != ELFMAG3)) {
85                 return "No ELF signature found on kernel\n";
86         }
87         if (ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
88                 return "Not a 64bit ELF kernel\n";
89         }
90         if (ehdr->e_ident[EI_DATA]  != ELFDATA2LSB) {
91                 return "Not a little endian ELF kernel\n";
92         }
93         if (le16_to_cpu(ehdr->e_type) != ET_EXEC) {
94                 return "Not an executable kernel\n";
95         }
96         if (le16_to_cpu(ehdr->e_machine) != EM_X86_64) {
97                 return "Not an x86_64 kernel\n";
98         }
99         if (    (ehdr->e_ident[EI_VERSION] != EV_CURRENT) ||
100                 (le32_to_cpu(ehdr->e_version) != EV_CURRENT)) {
101                 return "Kernel not using ELF version 1.\n";
102         }
103         if (le16_to_cpu(ehdr->e_phentsize) != sizeof(*phdr)) {
104                 return "Kernel uses bad program header size.\n";
105         }
106         phdr = (Elf64_Phdr *)(kernel_buf + le64_to_cpu(ehdr->e_phoff));
107         phdrs = 0;
108         for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
109                 if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
110                         continue;
111                 phdrs++;
112         }
113         if (phdrs == 0) {
114                 return "No PT_LOAD segments!\n";
115         }
116         parse_kernel_type = parse_elf64_kernel;
117         return 0;
118 }
119
120 char *bzImage_i386_probe(char *kernel_buf, off_t kernel_size)
121 {
122         struct x86_linux_header *hdr;
123         unsigned long offset;
124         int setup_sects;
125         hdr = (struct x86_linux_header *)kernel_buf;
126
127         if (le16_to_cpu(hdr->boot_sector_magic) != 0xaa55) {
128                 return "No bootsector magic";
129         }
130         if (memcmp(hdr->header_magic, "HdrS", 4) != 0) {
131                 return "Not a linux kernel";
132         }
133
134         if (le16_to_cpu(hdr->protocol_version) < 0x202) {
135                 return "Kernel protcols version before 2.02 not supported";
136         }
137
138         setup_sects = hdr->setup_sects;
139         if (setup_sects == 0) {
140                 setup_sects = 4;
141         }
142         offset = 512 + (512 *setup_sects);
143         if (offset > kernel_size) {
144                 return "Not enough bytes";
145         }
146         parse_kernel_type = parse_bzImage_kernel;
147         return 0;
148 }
149
150 char *linux_i386_probe(char *kernel_buf, off_t kernel_size)
151 {
152         char *result;
153         result = "";
154         if (result) result = bzImage_i386_probe(kernel_buf, kernel_size);
155         if (result) result = vmlinux_i386_probe(kernel_buf, kernel_size);
156         if (result) result = bzImage_i386_probe(kernel_buf, kernel_size);
157         return result;
158 }
159
160 struct kernel_info
161 {
162         int phdrs;
163         void *kernel[4];
164         size_t filesz[4];
165         size_t memsz[4];
166         size_t paddr[4];
167         size_t vaddr[4];
168         size_t entry;
169         size_t switch_64;
170         char *version;
171 };
172
173 static void parse_elf32_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
174 {
175         Elf32_Ehdr *ehdr;
176         Elf32_Phdr *phdr;
177         int i;
178         int phdrs;
179         ehdr = (Elf32_Ehdr *)kernel_buf;
180         phdr = (Elf32_Phdr *)(kernel_buf + ehdr->e_phoff);
181         phdrs = 0;
182         for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
183                 if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
184                         continue;
185                 info->kernel[phdrs]  = kernel_buf + le32_to_cpu(phdr[i].p_offset);
186                 info->filesz[phdrs]  = le32_to_cpu(phdr[i].p_filesz);
187                 info->memsz[phdrs]   = le32_to_cpu(phdr[i].p_memsz);
188                 info->paddr[phdrs]   = le32_to_cpu(phdr[i].p_paddr) & 0xfffffff;
189                 info->vaddr[phdrs]   = le32_to_cpu(phdr[i].p_vaddr);
190                 phdrs++;
191         }
192
193         if(!phdrs)
194                 die("We need at least one phdr\n");
195
196         info->phdrs = phdrs;
197         info->entry   = le32_to_cpu(ehdr->e_entry);
198         info->switch_64   = 0; //not convert from elf64
199         info->version = "unknown";
200 }
201
202 static void parse_elf64_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
203 {
204         Elf64_Ehdr *ehdr;
205         Elf64_Phdr *phdr;
206         int i;
207         int phdrs;
208         ehdr = (Elf64_Ehdr *)kernel_buf;
209         phdr = (Elf64_Phdr *)(kernel_buf + le64_to_cpu(ehdr->e_phoff));
210
211         phdrs = 0;
212         for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
213                 if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
214                         continue;
215                 info->kernel[phdrs]  = kernel_buf + le64_to_cpu(phdr[i].p_offset);
216                 info->filesz[phdrs]  = le64_to_cpu(phdr[i].p_filesz);
217                 info->memsz[phdrs]   = le64_to_cpu(phdr[i].p_memsz);
218                 info->paddr[phdrs]   = le64_to_cpu(phdr[i].p_paddr) & 0xffffff;
219                 info->vaddr[phdrs]   = le64_to_cpu(phdr[i].p_vaddr);
220                 phdrs++;
221         }
222
223         if(!phdrs)
224                 die("We need at least one phdr\n");
225         
226         info->phdrs = phdrs;
227         info->entry   = le64_to_cpu(ehdr->e_entry);
228 #if  0
229         if (info->entry != info->paddr[0]) {
230                 info->entry = info->paddr[0]; // we still have startup_32 there
231                 info->switch_64   = 0; //not convert from elf64
232         } else
233 #endif
234                 info->switch_64   = 1; //convert from elf64
235
236         info->version = "unknown";
237 }
238
239
240 static void parse_bzImage_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
241 {
242         struct x86_linux_header *hdr;
243         unsigned long offset;
244         int setup_sects;
245         hdr = (struct x86_linux_header *)kernel_buf;
246         setup_sects = hdr->setup_sects;
247         if (setup_sects == 0) {
248                 setup_sects = 4;
249         }
250         offset = 512 + (512 *setup_sects);
251
252         info->kernel[0]  = kernel_buf + offset;
253         info->filesz[0]  = kernel_size - offset;
254         info->memsz[0]   = 0x700000;
255         info->paddr[0]   = 0x100000;
256         info->vaddr[0]   = 0x100000;
257         info->phdrs = 1;
258         info->entry   = info->paddr[0];
259         info->switch_64   = 0; //not convert from elf64, even later bzImage become elf64, it still includes startup_32
260         info->version = kernel_buf + 512 + le16_to_cpu(hdr->kver_addr);
261 }
262
263 static void parse_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
264 {
265         memset(info, 0, sizeof(*info));
266         if (parse_kernel_type) {
267                 parse_kernel_type(info, kernel_buf, kernel_size);
268         }
269         else {
270                 die("Unknown kernel format");
271         }
272 }
273
274 void linux_i386_usage(void)
275 {
276         printf(
277                 "      --command-line=<string> Set the command line to <string>\n"
278                 "      --append=<string>       Set the command line to <string>\n"
279                 "      --initrd=<filename>     Set the initrd to <filename>\n"
280                 "      --ramdisk=<filename>    Set the initrd to <filename>\n"
281                 "      --ramdisk-base=<addr>   Set the initrd load address to <addr>\n"
282                 );
283         return;
284 }
285
286
287 #define OPT_CMDLINE        OPT_MAX+0
288 #define OPT_RAMDISK        OPT_MAX+1
289 #define OPT_RAMDISK_BASE   OPT_MAX+2
290
291 #define DEFAULT_RAMDISK_BASE (8*1024*1024)
292
293 int linux_i386_mkelf(int argc, char **argv, 
294         struct memelfheader *ehdr, char *kernel_buf, off_t kernel_size)
295 {
296         const char *ramdisk, *cmdline;
297         unsigned long ramdisk_base;
298         char *payload_buf, *ramdisk_buf;
299         off_t payload_size, ramdisk_size;
300         struct memelfphdr *phdr;
301         struct memelfnote *note;
302         struct kernel_info kinfo;
303         struct image_parameters *params;
304         int index;
305         int i;
306
307         int opt;
308         static const struct option options[] = {
309                 MKELF_OPTIONS
310                 { "command-line",    1, 0, OPT_CMDLINE },
311                 { "append",          1, 0, OPT_CMDLINE },
312                 { "initrd",          1, 0, OPT_RAMDISK },
313                 { "ramdisk",         1, 0, OPT_RAMDISK },
314                 { "ramdisk-base",    1, 0, OPT_RAMDISK_BASE },
315                 { 0 , 0, 0, 0 },
316         };
317         static const char short_options[] = MKELF_OPT_STR;
318
319         ramdisk_base = DEFAULT_RAMDISK_BASE;
320         ramdisk = 0;
321         cmdline="";
322
323         while((opt = getopt_long(argc, argv, short_options, options, 0)) != -1) {
324                 switch(opt) {
325                 case '?':
326                         error("Unknown option %s\n", argv[optind]);
327                         break;
328                 case OPT_RAMDISK_BASE:
329                 {
330                         char *end;
331                         unsigned long base;
332                         base = strtoul(optarg, &end, 0);
333                         if ((end == optarg) || (*end != '\0')) {
334                                 error("Invalid ramdisk base\n");
335                         }
336                         ramdisk_base = base;
337                 }
338                 case OPT_RAMDISK:
339                         ramdisk = optarg;
340                         break;
341                 case OPT_CMDLINE:
342                         cmdline = optarg;
343                         break;
344                 default:
345                         break;
346                 }
347         }
348         ehdr->ei_class  = ELFCLASS32;
349         ehdr->ei_data   = ELFDATA2LSB;
350         ehdr->e_type    = ET_EXEC;
351         ehdr->e_machine = EM_386;
352
353         /* locate the payload buffer */
354         payload_buf = payload;
355         payload_size = sizeof(payload);
356
357         /* slurp the input files */
358         ramdisk_buf = slurp_file(ramdisk, &ramdisk_size);
359
360         /* parse the kernel */
361         parse_kernel(&kinfo, kernel_buf, kernel_size);
362
363         /* Find the parameters */
364         params = (void *)(payload_buf + (payload_size - sizeof(*params)));
365
366         /* A sanity check against bad versions of binutils */
367         if (params->convert_magic != CONVERT_MAGIC) {
368                 die("Internal error convert_magic %08x != %08x\n",
369                         params->convert_magic, CONVERT_MAGIC);
370         }
371
372         /* Copy the command line */
373         strncpy(params->cmdline, cmdline, sizeof(params->cmdline));
374         params->cmdline[sizeof(params->cmdline)-1]= '\0';
375
376         
377         /* Add a program header for the note section */
378         index = 4;
379         index += (kinfo.phdrs - 1);
380         index += ramdisk_size ? 1:0;
381         phdr = add_program_headers(ehdr, index);
382
383         /* Fill in the program headers*/
384         phdr[0].p_type = PT_NOTE;
385         
386         /* Fill in the converter program headers */
387         phdr[1].p_paddr  = CONVERTLOC;
388         phdr[1].p_vaddr  = CONVERTLOC;
389         phdr[1].p_filesz = payload_size;
390         phdr[1].p_memsz  = payload_size + params->bss_size;
391         phdr[1].p_data   = payload;
392
393         /* Reserve space for the REAL MODE DATA segment AND the GDT segment */
394         phdr[2].p_paddr  = REAL_MODE_DATA_LOC;
395         phdr[2].p_vaddr  = REAL_MODE_DATA_LOC;
396         phdr[2].p_filesz = 0;
397         if(!kinfo.switch_64)
398                 phdr[2].p_memsz = (GDTLOC - REAL_MODE_DATA_LOC) + params->gdt_size;
399         else
400                 phdr[2].p_memsz = (PGTLOC - REAL_MODE_DATA_LOC) + params->pgt_size;
401         phdr[2].p_data   = 0;
402
403         if( (phdr[1].p_paddr + phdr[1].p_memsz) > phdr[2].p_paddr) {
404                 die("Internal error: need to increase REAL_MODE_DATA_LOC !\n");
405         }
406
407         index = 3;
408         /* Put the second kernel frament if present */
409         for(i=0;i<kinfo.phdrs;i++) {
410                 phdr[index].p_paddr  = kinfo.paddr[i];
411                 phdr[index].p_vaddr  = kinfo.vaddr[i];
412                 phdr[index].p_filesz = kinfo.filesz[i];
413                 phdr[index].p_memsz  = kinfo.memsz[i];
414                 phdr[index].p_data   = kinfo.kernel[i];
415                 index++;
416         }
417         
418         /* Put the ramdisk at ramdisk base.
419          */
420         params->initrd_start = params->initrd_size = 0;
421         if (ramdisk_size) {
422                 if( (phdr[index-1].p_paddr + phdr[index-1].p_memsz) > ramdisk_base) {
423                         die("need to increase increase ramdisk_base !\n");
424                 }
425
426                 phdr[index].p_paddr  = ramdisk_base;
427                 phdr[index].p_vaddr  = ramdisk_base;
428                 phdr[index].p_filesz = ramdisk_size;
429                 phdr[index].p_memsz  = ramdisk_size;
430                 phdr[index].p_data   = ramdisk_buf;
431                 params->initrd_start = phdr[index].p_paddr;
432                 params->initrd_size  = phdr[index].p_filesz;
433                 index++;
434         }
435         
436         /* Set the start location */
437         params->entry = kinfo.entry;
438         params->switch_64 = kinfo.switch_64;
439         ehdr->e_entry = phdr[1].p_paddr;
440
441         /* Setup the elf notes */
442         note = add_notes(ehdr, 3);
443         note[0].n_type = EIN_PROGRAM_NAME;
444         note[0].n_name = "ELFBoot";
445         note[0].n_desc = "Linux";
446         note[0].n_descsz = strlen(note[0].n_desc)+1;
447
448         note[1].n_type = EIN_PROGRAM_VERSION;
449         note[1].n_name = "ELFBoot";
450         note[1].n_desc = kinfo.version;
451         note[1].n_descsz = strlen(note[1].n_desc);
452
453         note[2].n_type = EIN_PROGRAM_CHECKSUM;
454         note[2].n_name = "ELFBoot";
455         note[2].n_desc = 0;
456         note[2].n_descsz = 2;
457
458         return 0;
459 }
460