2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
5 * Copyright (C) 2001 Silicon Graphics, Inc.
6 * Contributed by Brent Casavant <bcasavan@sgi.com>
8 * This file is part of the ELILO, the EFI Linux boot loader.
10 * ELILO is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * ELILO is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with ELILO; see the file COPYING. If not, write to the Free
22 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 * Please check out the elilo.txt for complete documentation on how
26 * to use this program.
40 #define memzero(s, n) Memset((VOID *)(s), 0, (n))
41 #define memcpy(a,b,n) Memcpy((VOID *)(a),(b),(n))
43 /* size of output buffer */
44 #define WSIZE 0x8000 /* Window size must be at least 32k, */
45 /* and a power of two */
46 /* size of input buffer */
47 #define INBUFSIZE 0x8000
54 #define FUNC_STATIC static
56 typedef unsigned char uch;
57 typedef unsigned short ush;
58 typedef unsigned long ulg;
61 typedef struct segment {
62 unsigned long addr; /* start address */
63 unsigned long offset; /* file offset */
64 unsigned long size; /* file size */
65 unsigned long bss_sz; /* BSS size */
66 UINT8 flags; /* indicates whether to load or not */
69 #define CHUNK_FL_VALID 0x1
70 #define CHUNK_FL_LOAD 0x2
72 #define CHUNK_CAN_LOAD(n) chunks[(n)].flags |= CHUNK_FL_LOAD
73 #define CHUNK_NO_LOAD(n) chunks[(n)].flags &= ~CHUNK_FL_LOAD
74 #define CHUNK_IS_LOAD(n) (chunks[(n)].flags & CHUNK_FL_LOAD)
76 #define CHUNK_VALIDATE(n) chunks[(n)].flags |= CHUNK_FL_VALID
77 #define CHUNK_INVALIDATE(n) chunks[(n)].flags = 0
78 #define CHUNK_IS_VALID(n) (chunks[(n)].flags & CHUNK_FL_VALID)
81 * static parameters to gzip helper functions
82 * we cannot use paramters because API was not
85 static segment_t *chunks; /* holds the list of segments */
86 static segment_t *cur_chunk;
88 static UINTN chunk; /* current segment */
89 static UINTN input_fd;
90 static VOID *kernel_entry, *kernel_base, *kernel_end;
92 static uch *inbuf; /* input buffer (compressed data) */
93 static uch *window; /* output buffer (uncompressed data) */
94 static unsigned long file_offset; /* position in the file */
96 static unsigned insize = 0; /* valid bytes in inbuf */
97 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
98 static unsigned outcnt = 0; /* bytes in output buffer */
101 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
102 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
103 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
104 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
105 #define COMMENT 0x10 /* bit 4 set: file comment present */
106 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
107 #define RESERVED 0xC0 /* bit 6,7: reserved */
109 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
111 /* Diagnostic functions */
113 # define Assert(cond,msg) {if(!(cond)) error(msg);}
115 # define Trace(x) Print(L"line %d:\n", __LINE__);
116 # define Tracev(x) {if (verbose) Print(L"line %d:\n", __LINE__) ;}
117 # define Tracevv(x) {if (verbose>1) Print(L"line %d:\n", __LINE__) ;}
118 # define Tracec(c,x) {if (verbose && (c)) Print(L"line %d:\n", __LINE__) ;}
119 # define Tracecv(c,x) {if (verbose>1 && (c)) Print(L"line %d:\n", __LINE__) ;}
121 # define Assert(cond,msg)
126 # define Tracecv(c,x)
129 static int fill_inbuf(void);
130 static void flush_window(void);
131 static void error(char *m);
132 static long bytes_out;
134 static void error(char *m);
137 static int error_return;
138 static UINTN elf_is_big_endian; /* true if ELF file is big endian */
141 gzip_malloc(int size)
143 return (void *)alloc(size, 0);
147 gzip_free(void *where)
155 * Fill the input buffer and return the first byte in it. This is called
156 * only when the buffer is empty and at least one byte is really needed.
161 INTN expected, nread;
164 expected = nread = INBUFSIZE;
166 status = fops_read(input_fd, inbuf, &nread);
167 if (EFI_ERROR(status)) {
168 error("elilo: Read failed");
170 DBG_PRT((L"%s : read %d bytes of %d bytes\n", LD_NAME, nread, expected));
178 /* ===========================================================================
179 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
180 * (Used for the decompressed data only.)
184 * Run a set of bytes through the crc shift register. If s is a NULL
185 * pointer, then initialize the crc shift register contents instead.
186 * Return the current crc in either case.
189 * S pointer to bytes to pump through.
190 * N number of bytes in S[].
193 updcrc(unsigned char *s, unsigned n)
195 register unsigned long c;
196 /* crc is defined in inflate.c */
203 c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
207 return c ^ 0xffffffffUL; /* (instead of ~c for 64-bit machines) */
212 * Clear input and output buffers
228 if(elf_is_big_endian) v = __ia64_swab64(v);
235 if(elf_is_big_endian) v = __ia64_swab32(v);
242 if(elf_is_big_endian) v = __ia64_swab16(v);
247 is_valid_header(Elf64_Ehdr *ehdr)
249 UINT16 type, machine;
251 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
252 type = __ia64_swab16(ehdr->e_type);
253 machine = __ia64_swab16(ehdr->e_machine);
256 machine = ehdr->e_machine;
258 VERB_PRT(3, Print(L"class=%d type=%d data=%d machine=%d\n",
259 ehdr->e_ident[EI_CLASS],
261 ehdr->e_ident[EI_DATA],
264 return ehdr->e_ident[EI_MAG0] == 0x7f
265 && ehdr->e_ident[EI_MAG1] == 'E'
266 && ehdr->e_ident[EI_MAG2] == 'L'
267 && ehdr->e_ident[EI_MAG3] == 'F'
268 && ehdr->e_ident[EI_CLASS] == ELFCLASS64
269 && type == ET_EXEC /* must be executable */
270 && machine == EM_IA_64 ? 0 : -1;
274 * will invalidate loadble segments which overlap with others
280 unsigned long iend = chunks[i].addr + chunks[i].size;
282 for(j=0; j < nchunks; j++) {
284 if (chunks[i].addr >= chunks[j].addr && iend < (chunks[j].addr + chunks[j].size)) {
285 DBG_PRT((L"%s : segment %d fully included in segment %d\n", LD_NAME, i, j));
286 CHUNK_INVALIDATE(i); /* nullyify segment */
297 for(i=0; i < nchunks; i++) {
298 if (CHUNK_IS_VALID(i) && !CHUNK_IS_LOAD(i)) check_overlap(i);
304 * The decompression code calls this function after decompressing the
305 * first block of the object file. The first block must contain all
306 * the relevant header information.
309 first_block (const char *buf, long blocksize)
313 UINTN total_size, pages;
314 UINTN low_addr, max_addr;
315 UINTN load_offset = 0;
321 elf = (Elf64_Ehdr *)buf;
323 if (is_valid_header(elf) == -1) return -1;
325 /* determine file endianess */
326 elf_is_big_endian = elf->e_ident[EI_DATA] == ELFDATA2MSB ? 1 : 0;
329 offs = bswap64(elf->e_phoff);
330 phnum = bswap16(elf->e_phnum);
333 Print(L"ELF file is %s\n", elf_is_big_endian ? L"big endian" : L"little endian");
334 Print(L"Entry point 0x%lx\n", bswap64(elf->e_entry));
335 Print(L"%d program headers\n", phnum);
336 Print(L"%d segment headers\n", bswap16(elf->e_shnum));
340 /* XXX: need to check on this */
341 if (offs + phnum * sizeof(*phdrs) > (unsigned) blocksize) {
342 ERR_PRT((L"%s : ELF program headers not in first block (%ld)\n", LD_NAME, offs));
346 kernel_entry = (void *)bswap64(elf->e_entry);
348 if (((UINTN)kernel_entry >> 61) != 0) {
349 ERR_PRT((L"%s: <<ERROR>> entry point is a virtual address 0x%lx : not supported anymore\n", LD_NAME, kernel_entry));
352 phdrs = (Elf64_Phdr *) (buf + offs);
358 * allocate chunk table
359 * Convention: a segment that does not need loading will
360 * have chunk[].addr = 0.
362 chunks = (void *)alloc(sizeof(struct segment)*phnum, 0);
363 if (chunks == NULL) {
364 ERR_PRT((L"%s : failed alloc chunks %r\n", LD_NAME));
369 * find lowest and higest virtual addresses
370 * don't assume FULLY sorted !
372 for (i = 0; i < phnum; ++i) {
375 * record chunk no matter what because no load may happen
376 * anywhere in archive, not just as the last segment
378 paddr = bswap64(phdrs[i].p_paddr);
379 memsz = bswap64(phdrs[i].p_memsz),
381 chunks[i].addr = paddr;
382 chunks[i].offset = bswap64(phdrs[i].p_offset);
383 chunks[i].size = bswap64(phdrs[i].p_filesz);
384 chunks[i].bss_sz = bswap64(phdrs[i].p_memsz) - bswap64(phdrs[i].p_filesz);
388 if (bswap32(phdrs[i].p_type) != PT_LOAD) {
389 CHUNK_NO_LOAD(i); /* mark no load chunk */
390 DBG_PRT((L"%s : skipping segment %ld\n", LD_NAME, i));
394 CHUNK_CAN_LOAD(i); /* mark no load chunk */
397 Print(L"\n%s : segment %ld vaddr [0x%lx-0x%lx] offset %ld filesz %ld memsz=%ld bss_sz=%ld\n",
401 chunks[i].addr+bswap64(phdrs[i].p_filesz),
407 if (paddr < low_addr) low_addr = paddr;
409 if (paddr + memsz > max_addr) max_addr = paddr + memsz;
412 if (low_addr & (EFI_PAGE_SIZE - 1)) {
413 ERR_PRT((L"%s : low_addr not page aligned 0x%lx\n", LD_NAME, low_addr));
419 DBG_PRT((L"%s : %d program headers entry=0x%lx\nlowest_addr=0x%lx highest_addr=0x%lx\n",
421 phnum, kernel_entry, low_addr, max_addr));
423 total_size = (UINTN)max_addr - (UINTN)low_addr;
424 pages = EFI_SIZE_TO_PAGES(total_size);
427 * Record end of kernel for initrd
429 kernel_base = (void *)low_addr;
430 kernel_end = (void *)(low_addr + (pages << EFI_PAGE_SHIFT));
432 /* allocate memory for the kernel */
433 if (alloc_kmem((void *)low_addr, pages) == -1) {
436 ERR_PRT((L"%s : AllocatePages(%d, 0x%lx) for kernel failed\n", LD_NAME, pages, low_addr));
438 if (ia64_can_relocate() == 0) {
439 ERR_PRT((L"relocation is disabled, cannot load kernel"));
444 * could not allocate at requested spot, try to find a
445 * suitable location to relocate the kernel
447 * The maximum sized Itanium TLB translation entry is 256 MB.
448 * If we relocate the kernel by this amount we know for sure
449 * that alignment constraints will be satisified, regardless
450 * of the kernel used.
452 VERB_PRT(1, Print(L"Attempting to relocate kernel.\n"));
454 if (find_kernel_memory((VOID*) low_addr, (VOID*) max_addr, 256*MB, &new_addr) == -1) {
455 ERR_PRT((L"%s : find_kernel_memory(0x%lx, 0x%lx, 0x%lx, 0x%lx) failed\n", LD_NAME, low_addr, max_addr, 256*MB, &load_offset));
458 /* unsigned arithmetic */
459 load_offset = (UINTN) (new_addr - ROUNDDOWN((UINTN) low_addr,256*MB));
461 ERR_PRT((L"low_addr=0x%lx new_addr=0x%lx offset=0x%lx", low_addr, new_addr, load_offset));
464 * correct various addresses for non-zero load_offset
466 kernel_base = (void *) ((UINTN) kernel_base + load_offset);
467 kernel_end = (void *) ((UINTN) kernel_end + load_offset);
468 kernel_entry = (void*) ((UINTN) kernel_entry + load_offset);
470 for (i = 0; i < phnum; ++i) {
471 chunks[i].addr += load_offset;
472 phdrs[i].p_paddr = (Elf64_Addr) ((UINT64) phdrs[i].p_paddr + load_offset);
476 * try one last time to get memory for the kernel
478 if (alloc_kmem((void *)low_addr+load_offset, pages) == -1) {
479 ERR_PRT((L"%s : AllocatePages(%d, 0x%lx) for kernel failed\n", LD_NAME, pages, low_addr+load_offset));
480 ERR_PRT((L"Relocation by 0x%lx bytes failed.\n", load_offset));
486 if (chunks) free(chunks);
491 * Determine which chunk in the Elf file will be coming out of the expand
501 for(i=0; i < nchunks; i++) {
503 if (!CHUNK_IS_VALID(i) || !CHUNK_IS_LOAD(i)) continue;
505 if (file_offset > chunks[i].offset) continue;
507 if (cp == NULL || chunks[i].offset < cp->offset) cp = &chunks[i];
514 * Write the output window window[0..outcnt-1] holding uncompressed
515 * data and update crc.
520 static const CHAR8 helicopter[4] = { '|' , '/' , '-' , '\\' };
521 static UINTN heli_count;
528 DBG_PRT((L"%s : flush_window outnct=%d file_offset=%ld\n", LD_NAME, outcnt, file_offset));
530 Print(L"%c\b",helicopter[heli_count++%4]);
532 updcrc(window, outcnt);
535 * first time, we extract the headers
538 if (first_block(window, outcnt) < 0) error("invalid exec header");
545 /* check if user wants to abort */
546 if (check_abort() == EFI_SUCCESS) goto load_abort;
549 if (cp == NULL || file_offset + outcnt <= cp->offset) {
550 file_offset += outcnt;
554 // Does this window begin before the current chunk?
555 if (file_offset < cp->offset) {
556 unsigned long skip = cp->offset - file_offset;
562 dst = (char *)cp->addr + (file_offset - cp->offset);
564 cnt = cp->offset + cp->size - file_offset;
566 if (cnt > outcnt) cnt = outcnt;
568 Memcpy(dst, src, cnt);
574 /* See if we are at the end of this chunk */
575 if (file_offset == cp->offset + cp->size) {
577 dst = (char *)cp->addr + cp->size;
578 Memset(dst, 0, cp->bss_sz);
581 /* handle remaining bytes */
582 if (outcnt) goto tail;
587 error_return = ELILO_LOAD_ABORTED;
594 ERR_PRT((L"%s : %a", LD_NAME, x));
595 /* will eventually exit with error from gunzip() */
600 decompress_kernel(VOID)
606 Print(L"Uncompressing Linux... ");
608 if (ret == 0) Print(L"done\n");
609 return ret == 0 ? 0 : -1;
613 gunzip_kernel(fops_fd_t fd, kdesc_t *kd)
617 error_return = ELILO_LOAD_ERROR;
619 window = (void *)alloc(WSIZE, 0);
620 if (window == NULL) {
621 ERR_PRT((L"%s : allocate output window failed\n", LD_NAME));
625 inbuf = (void *)alloc(INBUFSIZE, 0);
627 ERR_PRT((L"%s : allocate input window failedr\n", LD_NAME));
635 if (setjmp(jbuf) == 1) goto error;
638 ret = decompress_kernel();
641 if (window) free(window);
642 if (inbuf) free(inbuf);
645 kd->kentry = kernel_entry;
646 kd->kend = kernel_end;
647 kd->kstart = kernel_base;
648 error_return = ELILO_LOAD_SUCCESS;