5 #include "uniform_boot.h"
6 #include "linuxbios_tables.h"
9 #define STACK_SIZE (4096)
11 #define __unused __attribute__ ((unused))
13 long user_stack [STACK_SIZE] = { 0 };
15 unsigned long * stack_start = & user_stack[STACK_SIZE];
17 /* FIXME expand on drive_info_)struct... */
18 struct drive_info_struct {
21 struct sys_desc_table {
27 * These are set up by the setup-routine at boot-time:
31 uint8_t orig_x; /* 0x00 */
32 uint8_t orig_y; /* 0x01 */
33 uint16_t dontuse1; /* 0x02 -- EXT_MEM_K sits here */
34 uint16_t orig_video_page; /* 0x04 */
35 uint8_t orig_video_mode; /* 0x06 */
36 uint8_t orig_video_cols; /* 0x07 */
37 uint16_t unused2; /* 0x08 */
38 uint16_t orig_video_ega_bx; /* 0x0a */
39 uint16_t unused3; /* 0x0c */
40 uint8_t orig_video_lines; /* 0x0e */
41 uint8_t orig_video_isVGA; /* 0x0f */
42 uint16_t orig_video_points; /* 0x10 */
44 /* VESA graphic mode -- linear frame buffer */
45 uint16_t lfb_width; /* 0x12 */
46 uint16_t lfb_height; /* 0x14 */
47 uint16_t lfb_depth; /* 0x16 */
48 uint32_t lfb_base; /* 0x18 */
49 uint32_t lfb_size; /* 0x1c */
50 uint16_t dontuse2, dontuse3; /* 0x20 -- CL_MAGIC and CL_OFFSET here */
51 uint16_t lfb_linelength; /* 0x24 */
52 uint8_t red_size; /* 0x26 */
53 uint8_t red_pos; /* 0x27 */
54 uint8_t green_size; /* 0x28 */
55 uint8_t green_pos; /* 0x29 */
56 uint8_t blue_size; /* 0x2a */
57 uint8_t blue_pos; /* 0x2b */
58 uint8_t rsvd_size; /* 0x2c */
59 uint8_t rsvd_pos; /* 0x2d */
60 uint16_t vesapm_seg; /* 0x2e */
61 uint16_t vesapm_off; /* 0x30 */
62 uint16_t pages; /* 0x32 */
66 #define PAGE_SIZE 4096
69 #define E820MAP 0x2d0 /* our map */
70 #define E820MAX 32 /* number of entries in E820MAP */
71 #define E820NR 0x1e8 /* # entries in E820MAP */
75 unsigned long long addr; /* start of memory segment */
76 unsigned long long size; /* size of memory segment */
77 unsigned long type; /* type of memory segment */
79 #define E820_RESERVED 2
80 #define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */
86 struct e820entry map[E820MAX];
90 struct apm_bios_info {
91 uint16_t version; /* 0x40 */
92 uint16_t cseg; /* 0x42 */
93 uint32_t offset; /* 0x44 */
94 uint16_t cseg_16; /* 0x48 */
95 uint16_t dseg; /* 0x4a */
96 uint16_t flags; /* 0x4c */
97 uint16_t cseg_len; /* 0x4e */
98 uint16_t cseg_16_len; /* 0x50 */
99 uint16_t dseg_len; /* 0x52 */
100 uint8_t reserved[44]; /* 0x54 */
105 uint8_t orig_x; /* 0x00 */
106 uint8_t orig_y; /* 0x01 */
107 uint16_t ext_mem_k; /* 0x02 -- EXT_MEM_K sits here */
108 uint16_t orig_video_page; /* 0x04 */
109 uint8_t orig_video_mode; /* 0x06 */
110 uint8_t orig_video_cols; /* 0x07 */
111 uint16_t unused2; /* 0x08 */
112 uint16_t orig_video_ega_bx; /* 0x0a */
113 uint16_t unused3; /* 0x0c */
114 uint8_t orig_video_lines; /* 0x0e */
115 uint8_t orig_video_isVGA; /* 0x0f */
116 uint16_t orig_video_points; /* 0x10 */
118 /* VESA graphic mode -- linear frame buffer */
119 uint16_t lfb_width; /* 0x12 */
120 uint16_t lfb_height; /* 0x14 */
121 uint16_t lfb_depth; /* 0x16 */
122 uint32_t lfb_base; /* 0x18 */
123 uint32_t lfb_size; /* 0x1c */
124 uint16_t cl_magic; /* 0x20 */
125 #define CL_MAGIC_VALUE 0xA33F
126 uint16_t cl_offset; /* 0x22 */
127 uint16_t lfb_linelength; /* 0x24 */
128 uint8_t red_size; /* 0x26 */
129 uint8_t red_pos; /* 0x27 */
130 uint8_t green_size; /* 0x28 */
131 uint8_t green_pos; /* 0x29 */
132 uint8_t blue_size; /* 0x2a */
133 uint8_t blue_pos; /* 0x2b */
134 uint8_t rsvd_size; /* 0x2c */
135 uint8_t rsvd_pos; /* 0x2d */
136 uint16_t vesapm_seg; /* 0x2e */
137 uint16_t vesapm_off; /* 0x30 */
138 uint16_t pages; /* 0x32 */
139 uint8_t reserved4[12]; /* 0x34 -- 0x3f reserved for future expansion */
141 struct apm_bios_info apm_bios_info; /* 0x40 */
142 struct drive_info_struct drive_info; /* 0x80 */
143 struct sys_desc_table sys_desc_table; /* 0xa0 */
144 uint32_t alt_mem_k; /* 0x1e0 */
145 uint8_t reserved5[4]; /* 0x1e4 */
146 uint8_t e820_map_nr; /* 0x1e8 */
147 uint8_t reserved6[9]; /* 0x1e9 */
148 uint16_t mount_root_rdonly; /* 0x1f2 */
149 uint8_t reserved7[4]; /* 0x1f4 */
150 uint16_t ramdisk_flags; /* 0x1f8 */
151 #define RAMDISK_IMAGE_START_MASK 0x07FF
152 #define RAMDISK_PROMPT_FLAG 0x8000
153 #define RAMDISK_LOAD_FLAG 0x4000
154 uint8_t reserved8[2]; /* 0x1fa */
155 uint16_t orig_root_dev; /* 0x1fc */
156 uint8_t reserved9[1]; /* 0x1fe */
157 uint8_t aux_device_info; /* 0x1ff */
158 uint8_t reserved10[2]; /* 0x200 */
159 uint8_t param_block_signature[4]; /* 0x202 */
160 uint16_t param_block_version; /* 0x206 */
161 uint8_t reserved11[8]; /* 0x208 */
162 uint8_t loader_type; /* 0x210 */
163 #define LOADER_TYPE_LOADLIN 1
164 #define LOADER_TYPE_BOOTSECT_LOADER 2
165 #define LOADER_TYPE_SYSLINUX 3
166 #define LOADER_TYPE_ETHERBOOT 4
167 #define LOADER_TYPE_KERNEL 5
168 uint8_t loader_flags; /* 0x211 */
169 uint8_t reserved12[2]; /* 0x212 */
170 uint32_t kernel_start; /* 0x214 */
171 uint32_t initrd_start; /* 0x218 */
172 uint32_t initrd_size; /* 0x21c */
173 uint8_t reserved13[4]; /* 0x220 */
175 uint16_t heap_end_ptr; /* 0x224 */
176 uint8_t reserved14[2]; /* 0x226 */
178 uint32_t cmd_line_ptr; /* 0x228 */
180 uint32_t initrd_addr_max; /* 0x22c */
181 uint8_t reserved15[0x2d0 - 0x230]; /* 0x230 */
183 struct e820entry e820_map[E820MAX]; /* 0x2d0 */
184 uint8_t reserved16[688]; /* 0x550 */
185 #define COMMAND_LINE_SIZE 256
186 uint8_t command_line[COMMAND_LINE_SIZE]; /* 0x800 */
187 uint8_t reserved17[1792]; /* 0x900 - 0x1000 */
190 /* Keep track of which information I need to acquire. */
195 struct image_parameters *image;
196 struct parameters *real_mode;
197 /* bootloader type */
199 int has_uniform_boot;
204 struct lb_header *lb_table;
205 /* machine information needed */
209 * This is set up by the setup-routine at boot-time
216 #define memzero(s, n) memset ((s), 0, (n))
219 /* FIXME handle systems with large EBDA's */
220 static struct parameters *faked_real_mode = (void *)REAL_MODE_DATA_LOC;
226 * =============================================================================
231 #define TTYS0_LSR (TTYS0+0x05)
232 #define TTYS0_TBR (TTYS0+0x00)
234 static void ttys0_tx_byte(unsigned byte)
236 /* Wait until I can send a byte */
237 while((inb(TTYS0_LSR) & 0x20) == 0)
239 outb(byte, TTYS0_TBR);
240 /* Wait until the byte is transmitted */
241 while(!(inb(TTYS0_LSR) & 0x40))
244 static void put_char_serial(int c)
252 static void putchar(int c)
260 #define LONG_LONG_SHIFT ((int)((sizeof(unsigned long long)*CHAR_BIT) - 4))
261 #define LONG_SHIFT ((int)((sizeof(unsigned long)*CHAR_BIT) - 4))
262 #define INT_SHIFT ((int)((sizeof(unsigned int)*CHAR_BIT) - 4))
263 #define SHRT_SHIFT ((int)((sizeof(unsigned short)*CHAR_BIT) - 4))
264 #define CHAR_SHIFT ((int)((sizeof(unsigned char)*CHAR_BIT) - 4))
266 /**************************************************************************
270 %x - 4 bytes int (8 hex digits, lower case)
271 %X - 4 bytes int (8 hex digits, upper case)
272 %lx - 8 bytes long (16 hex digits, lower case)
273 %lX - 8 bytes long (16 hex digits, upper case)
274 %hx - 2 bytes int (4 hex digits, lower case)
275 %hX - 2 bytes int (4 hex digits, upper case)
276 %hhx - 1 byte int (2 hex digits, lower case)
277 %hhX - 1 byte int (2 hex digits, upper case)
278 - optional # prefixes 0x or 0X
282 Note: width specification not supported
283 **************************************************************************/
284 static void printf(const char *fmt, ...)
289 for ( ; *fmt != '\0'; ++fmt) {
295 for(p = va_arg(args, char *); *p != '\0'; p++)
298 else { /* Length of item is bounded */
299 char tmp[40], *q = tmp;
300 int shift = INT_SHIFT;
302 shift = LONG_LONG_SHIFT;
305 else if (*fmt == 'l') {
309 else if (*fmt == 'h') {
319 * Before each format q points to tmp buffer
320 * After each format q points past end of item
322 if ((*fmt | 0x20) == 'x') {
323 /* With x86 gcc, sizeof(long) == sizeof(int) */
324 unsigned long long h;
326 if (shift > LONG_SHIFT) {
327 h = va_arg(args, unsigned long long);
329 else if (shift > INT_SHIFT) {
330 h = va_arg(args, unsigned long);
332 h = va_arg(args, unsigned int);
334 ncase = (*fmt & 0x20);
335 for ( ; shift >= 0; shift -= 4)
336 *q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
338 else if (*fmt == 'd') {
341 if (shift > LONG_SHIFT) {
342 i = va_arg(args, long long);
344 else if (shift > INT_SHIFT) {
345 i = va_arg(args, long);
347 i = va_arg(args, int);
353 p = q; /* save beginning of digits */
355 *q++ = '0' + (i % 10);
358 /* reverse digits, stop in middle */
359 r = q; /* don't alter q */
366 else if (*fmt == 'c')
367 *q++ = va_arg(args, int);
370 /* now output the saved string */
371 for (p = tmp; p < q; ++p)
380 * =============================================================================
384 size_t strnlen(const char *s, size_t max)
387 while(len < max && *s) {
394 void* memset(void* s, int c, size_t n)
399 for (i=0;i<n;i++) ss[i] = c;
403 void* memcpy(void *dest, const void *src, size_t len)
407 const unsigned char *s;
411 for (i=0; i < len; i++)
417 int memcmp(void *src1, void *src2, size_t len)
419 unsigned char *s1, *s2;
423 for(i = 0; i < len; i++) {
434 * =============================================================================
438 static unsigned long checksum_partial(unsigned long sum,
439 void *addr, unsigned long length)
447 /* In the most straight forward way possible,
448 * compute an ip style checksum.
452 for(i = 0; i < length; i++) {
458 /* Add the new value */
460 /* Wrap around the carry */
462 sum = (sum + (sum >> 16)) & 0xFFFF;
465 value.byte[0] = sum & 0xff;
466 value.byte[1] = (sum >> 8) & 0xff;
467 return value.word & 0xFFFF;
471 static unsigned long checksum_final(unsigned long partial)
473 return (~partial) & 0xFFFF;
476 static unsigned long compute_checksum(void *vaddr, unsigned long length)
478 return checksum_final(checksum_partial(0, vaddr, length));
483 * =============================================================================
487 void append_command_line(struct parameters *real_mode, char *arg, int arg_bytes)
491 /* skip over what has already been set */
492 len = strnlen(real_mode->command_line, sizeof(real_mode->command_line));
493 dest = real_mode->command_line + len;
494 max = sizeof(real_mode->command_line) - 1;
496 /* No room to append anything :( */
499 /* Add a space in between strings */
501 /* Append the added command line */
502 max = sizeof(real_mode->command_line) - 1;
503 if (max > arg_bytes) {
506 len = strnlen(arg, max);
507 memcpy(dest, arg, len);
509 /* Null terminate the string */
513 static void set_memsize_k(struct parameters *real_mode, unsigned long mem_k)
515 /* ALT_MEM_K maxes out at 4GB */
516 if (mem_k > 0x3fffff) {
519 if (mem_k > (real_mode->alt_mem_k + (1 << 10))) {
520 /* Use our memory size less 1M */
521 real_mode->alt_mem_k = mem_k - (1 << 10);
522 real_mode->ext_mem_k = mem_k - (1 << 10);
523 if ((mem_k - (1 << 10)) > 0xFFFF) {
524 real_mode->ext_mem_k = 0xFC00; /* 64 M */
529 static void add_e820_map(struct parameters *real_mode,
530 unsigned long long addr, unsigned long long size,
533 unsigned long long high;
536 i = real_mode->e820_map_nr;
538 real_mode->e820_map[i].addr = addr;
539 real_mode->e820_map[i].size = size;
540 real_mode->e820_map[i].type = type;
541 real_mode->e820_map_nr++;
543 /* policy I assume that for the legacy memory
544 * variables memory is contiguous.
546 if (type == E820_RAM) {
548 if (high >= 0x40000000000ULL) {
553 set_memsize_k(real_mode, mem_k);
559 * =============================================================================
562 #define MULTI_MEM_DEBUG 0
564 #define multi_puts(x) printf("%s", x)
565 #define multi_put_hex(x) printf("%x", x)
566 #define multi_put_lhex(x) printf("%Lx", x)
568 #define multi_puts(x)
569 #define multi_put_hex(x)
570 #define multi_put_lhex(x)
571 #endif /* MULTI_MEM_DEBUG */
573 /* Multiboot Specification */
574 struct multiboot_mods {
577 unsigned char *string;
581 struct memory_segment {
582 unsigned long long addr;
583 unsigned long long size;
587 struct multiboot_info {
589 #define MULTIBOOT_MEM_VALID 0x01
590 #define MULTIBOOT_BOOT_DEV_VALID 0x02
591 #define MULTIBOOT_CMDLINE_VALID 0x04
592 #define MULTIBOOT_MODS_VALID 0x08
593 #define MULTIBOOT_AOUT_SYMS_VALID 0x10
594 #define MULTIBOOT_ELF_SYMS_VALID 0x20
595 #define MULTIBOOT_MMAP_VALID 0x40
598 unsigned char boot_device[4];
601 struct multiboot_mods *mods_addr;
606 unsigned mmap_length;
607 struct memory_segment *mmap_addr;
610 #define MULTIBOOT_MAX_COMMAND_LINE 0xFFFFFFFF
612 static void convert_multiboot_memmap(
613 struct parameters *real_mode,
614 struct multiboot_info *info)
618 #define next_seg(seg, size) ((struct memory_segment *)((char *)(seg) + (size)))
619 struct memory_segment *seg, *end;
621 seg = info->mmap_addr;
622 end = (void *)(((char *)seg) + info->mmap_length);
623 size_addr = (unsigned *)(((char *)seg) - 4);
625 multi_puts("mmap_addr: "); multi_put_hex((unsigned)info->mmap_addr); multi_puts("\n");
626 multi_puts("mmap_length: "); multi_put_hex(info->mmap_length); multi_puts("\n");
627 multi_puts("size_addr: "); multi_put_hex((unsigned)size_addr); multi_puts("\n");
628 multi_puts("size: "); multi_put_hex(size); multi_puts("\n");
629 multi_puts("end: "); multi_put_hex((unsigned)end); multi_puts("\n");
630 for(seg = info->mmap_addr; (seg < end); seg = next_seg(seg,size)) {
631 multi_puts("multi-mem: ");
632 multi_put_lhex(seg->size);
634 multi_put_lhex(seg->addr);
641 multi_puts("ACPI data");
644 multi_puts("ACPI NVS");
648 multi_puts("reserved");
652 add_e820_map(real_mode, seg->addr, seg->size, seg->type);
657 static void convert_multiboot(
658 struct param_info *info, struct multiboot_info *mb_info)
660 if (info->need_mem_sizes && (mb_info->flags & MULTIBOOT_MEM_VALID)) {
661 /* info->memory is short 1M */
662 set_memsize_k(info->real_mode, mb_info->mem_upper + (1 << 10));
664 if (mb_info->flags & MULTIBOOT_CMDLINE_VALID) {
665 append_command_line(info->real_mode, mb_info->command_line,
666 MULTIBOOT_MAX_COMMAND_LINE);
668 if (info->need_mem_sizes && (mb_info->flags & MULTIBOOT_MMAP_VALID)) {
669 convert_multiboot_memmap(info->real_mode, mb_info);
671 if (mb_info->flags & (MULTIBOOT_MEM_VALID | MULTIBOOT_MMAP_VALID)) {
672 info->need_mem_sizes = 0;
678 * Uniform Boot Environment
679 * =============================================================================
682 #define UBE_MEM_DEBUG 0
684 #define ube_puts(x) printf("%s", x)
685 #define ube_put_hex(x) printf("%x", x)
686 #define ube_put_lhex(x) printf("%Lx", x)
689 #define ube_put_hex(x)
690 #define ube_put_lhex(x)
691 #endif /* UBE_MEM_DEBUG */
692 static void convert_uniform_boot_memory(
693 struct parameters *real_mode, struct ube_memory *mem)
699 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
700 for(i = 0; (i < entries) && (i < E820MAX); i++) {
702 ube_puts("ube-mem: ");
703 ube_put_lhex(mem->map[i].size);
705 ube_put_lhex(mem->map[i].start);
707 switch(mem->map[i].type) {
714 ube_puts("ACPI data");
718 ube_puts("ACPI NVS");
720 case UBE_MEM_RESERVED:
722 type = E820_RESERVED;
723 ube_puts("reserved");
727 add_e820_map(real_mode,
728 mem->map[i].start, mem->map[i].size, type);
732 static void convert_uniform_boot(struct param_info *info,
733 struct uniform_boot_header *header)
735 /* Uniform boot environment */
736 unsigned long env_bytes;
738 if (header->arg_bytes) {
739 append_command_line(info->real_mode, (void *)(header->arg), header->arg_bytes);
741 env = (void *)(header->env);
742 env_bytes = header->env_bytes;
744 struct ube_record *record;
745 record = (void *)env;
746 if (record->tag == UBE_TAG_MEMORY) {
747 if (info->need_mem_sizes) {
748 convert_uniform_boot_memory(info->real_mode, (void *)record);
749 info->need_mem_sizes = 0;
753 env_bytes -= record->size;
762 * =============================================================================
765 /* we're getting screwed again and again by this problem of the 8259.
766 * so we're going to leave this lying around for inclusion into
767 * crt0.S on an as-needed basis.
768 * well, that went ok, I hope. Now we have to reprogram the interrupts :-(
769 * we put them right after the intel-reserved hardware interrupts, at
770 * int 0x20-0x2F. There they won't mess up anything. Sadly IBM really
771 * messed this up with the original PC, and they haven't been able to
772 * rectify it afterwards. Thus the bios puts interrupts at 0x08-0x0f,
773 * which is used for the internal hardware interrupts as well. We just
774 * have to reprogram the 8259's, and it isn't fun.
777 static void setup_i8259(void)
779 outb(0x11, 0x20); /*! initialization sequence to 8259A-1*/
780 outb(0x11, 0xA0); /*! and to 8259A-2*/
782 outb(0x20, 0x21); /*! start of hardware int's (0x20)*/
783 outb(0x28, 0xA1); /*! start of hardware int's 2 (0x28)*/
785 outb(0x04, 0x21); /*! 8259-1 is master*/
786 outb(0x02, 0xA1); /*! 8259-2 is slave*/
788 outb(0x01, 0x21); /*! 8086 mode for both*/
791 outb(0xFF, 0xA1); /*! mask off all interrupts for now*/
792 outb(0xFB, 0x21); /*! mask all irq's but irq2 which is cascaded*/
795 static void hardware_setup(struct param_info *info __unused)
800 /* Make sure any coprocessor is properly reset.. */
810 * =============================================================================
813 static int count_elf_notes(Elf_Bhdr *bhdr)
815 unsigned char *note, *end;
818 note = ((char *)bhdr) + sizeof(*bhdr);
819 end = ((char *)bhdr) + bhdr->b_size;
821 printf("count_elf_notes %lx\n", (unsigned long)bhdr);
825 unsigned char *n_name, *n_desc, *next;
826 hdr = (Elf_Nhdr *)note;
827 n_name = note + sizeof(*hdr);
828 n_desc = n_name + ((hdr->n_namesz + 3) & ~3);
829 next = n_desc + ((hdr->n_descsz + 3) & ~3);
831 printf("elf_note = %lx\n", (unsigned long)note);
832 printf("elf_namesz = %x\n", hdr->n_namesz);
833 printf("elf_descsz = %x\n", hdr->n_descsz);
834 printf("elf_type = %x\n", hdr->n_type);
835 printf("elf_name = %lx\n", (unsigned long)n_name);
836 printf("elf_desc = %lx\n", (unsigned long)n_desc);
846 static Elf_Nhdr *find_elf_note(Elf_Bhdr *bhdr,
847 Elf_Word namesz, unsigned char *name, Elf_Word type)
849 unsigned char *note, *end;
850 note = ((char *)bhdr) + sizeof(*bhdr);
851 end = ((char *)bhdr) + bhdr->b_size;
854 unsigned char *n_name, *n_desc, *next;
855 hdr = (Elf_Nhdr *)note;
856 n_name = note + sizeof(*hdr);
857 n_desc = n_name + ((hdr->n_namesz + 3) & ~3);
858 next = n_desc + ((hdr->n_descsz + 3) & ~3);
861 if ((hdr->n_type == type) &&
862 (hdr->n_namesz == namesz) &&
863 (memcmp(n_name, name, namesz) == 0)) {
871 static void convert_elf_command_line(struct param_info *info,
872 Elf_Word descsz, unsigned char *desc)
874 append_command_line(info->real_mode, desc, descsz);
881 void (*convert)(struct param_info *info, Elf_Word descsz, unsigned char *desc);
884 { 0, "", EBN_COMMAND_LINE, convert_elf_command_line },
887 static void convert_elf_boot(struct param_info *info, Elf_Bhdr *bhdr)
889 unsigned char *note, *end;
890 note = ((char *)bhdr) + sizeof(*bhdr);
891 end = ((char *)bhdr) + bhdr->b_size;
894 unsigned char *n_name, *n_desc, *next;
896 hdr = (Elf_Nhdr *)note;
897 n_name = note + sizeof(*hdr);
898 n_desc = n_name + ((hdr->n_namesz + 3) & ~3);
899 next = n_desc + ((hdr->n_descsz + 3) & ~3);
902 for(i = 0; i < sizeof(elf_notes)/sizeof(elf_notes[0]); i++) {
903 if ((hdr->n_type == elf_notes[i].type) &&
904 (hdr->n_namesz == elf_notes[i].namesz) &&
905 (memcmp(n_name, elf_notes[i].name, elf_notes[i].namesz) == 0)) {
906 elf_notes[i].convert(info, hdr->n_descsz, n_desc);
916 * =============================================================================
919 #define LB_MEM_DEBUG 0
921 #define lb_puts(x) printf("%s", x)
922 #define lb_put_hex(x) printf("%x", x)
923 #define lb_put_lhex(x) printf("%Lx", x)
926 #define lb_put_hex(x)
927 #define lb_put_lhex(x)
928 #endif /* LB_MEM_DEBUG */
930 static unsigned count_lb_records(void *start, unsigned long length)
932 struct lb_record *rec;
936 end = ((char *)start) + length;
937 for(rec = start; ((void *)rec < end) &&
938 (rec->size <= (unsigned long)(end - (void *)rec));
939 rec = (void *)(((char *)rec) + rec->size)) {
945 static struct lb_header *__find_lb_table(void *start, void *end)
948 /* For now be stupid.... */
949 for(ptr = start; (void *)ptr < end; ptr += 16) {
950 struct lb_header *head = (void *)ptr;
951 if ((head->signature[0] == 'L') &&
952 (head->signature[1] == 'B') &&
953 (head->signature[2] == 'I') &&
954 (head->signature[3] == 'O') &&
955 (head->header_bytes == sizeof(*head)) &&
956 (compute_checksum(head, sizeof(*head)) == 0) &&
957 (compute_checksum(ptr + sizeof(*head), head->table_bytes) ==
958 head->table_checksum) &&
959 (count_lb_records(ptr + sizeof(*head), head->table_bytes) ==
968 static int find_lb_table(struct param_info *info)
970 struct lb_header *head;
973 /* First try at address 0 */
974 head = __find_lb_table((void *)0x00000, (void *)0x1000);
977 /* Then try at address 0xf0000 */
978 head = __find_lb_table((void *)0xf0000, (void *)0x100000);
981 struct lb_forward *forward = (struct lb_forward *)(((char *)head) + head->header_bytes);
982 if (forward->tag == LB_TAG_FORWARD) {
983 head = __find_lb_table(forward->forward,
984 forward->forward + 0x1000);
988 info->has_linuxbios = 1;
989 info->lb_table = head;
995 static void convert_lb_memory(struct param_info *info, struct lb_memory *mem)
999 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
1000 for(i = 0; (i < entries) && (i < E820MAX); i++) {
1002 unsigned long long end;
1003 end = mem->map[i].start + mem->map[i].size;
1004 lb_puts("lb-mem: ");
1005 lb_put_lhex(mem->map[i].start);
1009 switch(mem->map[i].type) {
1015 type = E820_RESERVED;
1016 lb_puts("reserved");
1020 add_e820_map(info->real_mode,
1021 mem->map[i].start, mem->map[i].size, type);
1023 info->need_mem_sizes = 0;
1026 static void query_lb_values(struct param_info *info)
1028 struct lb_header *head;
1029 struct lb_record *rec;
1031 head = info->lb_table;
1032 start = ((unsigned char *)head) + sizeof(*head);
1033 end = ((char *)start) + head->table_bytes;
1034 for(rec = start; ((void *)rec < end) &&
1035 (rec->size <= (unsigned long)(end - (void *)rec));
1036 rec = (void *)(((char *)rec) + rec->size)) {
1040 struct lb_memory *mem;
1041 mem = (struct lb_memory *) rec;
1042 convert_lb_memory(info, mem);
1053 * =============================================================================
1055 #define PC_MEM_DEBUG 0
1057 #define pc_puts(x) printf("%s", x)
1058 #define pc_put_hex(x) printf("%x", x)
1059 #define pc_put_lhex(x) printf("%Lx", x)
1062 #define pc_put_hex(x)
1063 #define pc_put_lhex(x)
1064 #endif /* PC_MEM_DEBUG */
1066 /* functions for querying the pcbios */
1067 extern void noop(void); /* for testing purposes only */
1068 extern int meme820(struct e820entry *buf, int count);
1069 extern unsigned int meme801(void);
1070 extern unsigned short mem88(void);
1071 extern unsigned short basememsize(void);
1075 struct e820entry map[E820MAX];
1078 static struct meminfo meminfo;
1079 static void get_meminfo(struct param_info *info)
1082 pc_puts("getting meminfo...\n");
1083 meminfo.map_count = meme820(meminfo.map, E820MAX);
1084 pc_puts("got meminfo count="); pc_put_hex(meminfo.map_count); pc_puts("\n");
1085 for(i = 0; i < meminfo.map_count; i++) {
1086 unsigned long long end;
1087 struct e820entry *seg = meminfo.map + i;
1088 end = seg->addr + seg->size;
1089 pc_puts("BIOS-e820: ");
1090 pc_put_lhex(seg->addr);
1097 info->need_mem_sizes = 0;
1100 pc_puts("ACPI data");
1103 pc_puts("ACPI NVS");
1107 pc_puts("reserved");
1111 add_e820_map(info->real_mode,
1112 seg->addr, seg->size, seg->type);
1114 info->real_mode->alt_mem_k = meme801();
1115 info->real_mode->ext_mem_k = mem88();
1116 if (info->real_mode->alt_mem_k || info->real_mode->ext_mem_k) {
1117 info->need_mem_sizes = 0;
1121 static void query_pcbios_values(struct param_info *info)
1128 * =============================================================================
1132 static void query_bootloader_param_class(struct param_info *info)
1134 int has_bootloader_type = 0;
1136 if (!has_bootloader_type && (info->type == 0x2BADB002)) {
1137 /* Orignal multiboot specification */
1138 info->has_multiboot = 1;
1139 has_bootloader_type = 1;
1141 if (!has_bootloader_type && (info->type == 0x0A11B007)) {
1142 /* Uniform boot proposal */
1143 unsigned long checksum;
1144 struct uniform_boot_header *header;
1145 header = info->data;
1146 checksum = compute_checksum(header, header->header_bytes);
1147 if (checksum == 0) {
1148 info->has_uniform_boot = 1;
1149 has_bootloader_type = 1;
1151 printf("Bad uniform boot header checksum!\n");
1154 if (info->type == ELF_BHDR_MAGIC) {
1157 if (info->param && (info->param->b_signature == ELF_BHDR_MAGIC)) {
1160 if (!has_bootloader_type && hdr) {
1161 /* Good ELF boot proposal... */
1162 unsigned long checksum;
1165 if (hdr->b_checksum != 0) {
1166 checksum = compute_checksum(hdr, hdr->b_size);
1168 count = count_elf_notes(hdr);
1169 if ((hdr->b_signature == ELF_BHDR_MAGIC) &&
1171 hdr->b_records == count) {
1172 info->has_elf_boot = 1;
1174 has_bootloader_type = 1;
1177 printf("Bad ELF parameter table!\n");
1178 printf(" checksum = %x\n", checksum);
1179 printf(" count = %x\n", count);
1180 printf(" hdr = %x\n", (unsigned long)hdr);
1181 printf(" b_size = %x\n", hdr->b_size);
1182 printf("b_signature = %x\n", hdr->b_signature);
1183 printf(" b_records = %x\n", hdr->b_records);
1186 if (!has_bootloader_type) {
1187 printf("Unknown bootloader class!\n");
1188 printf("type=%x\n", info->type);
1189 printf("data=%x\n", (unsigned)info->data);
1190 printf("param=%x\n", (unsigned)info->param);
1194 static void query_bootloader_values(struct param_info *info)
1196 if (info->has_multiboot) {
1197 convert_multiboot(info, info->data);
1199 else if (info->has_uniform_boot) {
1200 convert_uniform_boot(info, info->data);
1202 else if (info->has_elf_boot) {
1203 convert_elf_boot(info, info->param);
1209 * =============================================================================
1212 static int bootloader_query_firmware_class(struct param_info *info)
1215 unsigned char *note, *n_name, *n_desc;
1216 int detected_firmware_type;
1217 if (!info->has_elf_boot) {
1218 /* Only the elf boot tables gives us a firmware type */
1221 detected_firmware_type = 0;
1224 hdr = find_elf_note(info->param, 0, 0, EBN_FIRMWARE_TYPE);
1226 /* If I'm not explicitly told the firmware type
1227 * do my best to guess it for myself.
1229 detected_firmware_type = 0;
1232 n_name = note + sizeof(*hdr);
1233 n_desc = n_name + ((hdr->n_namesz + 3) & ~3);
1235 if (!detected_firmware_type && hdr &&
1236 (hdr->n_descsz == 7) &&
1237 (memcmp(n_desc, "PCBIOS", 7) == 0)) {
1238 info->has_pcbios = 1;
1239 detected_firmware_type = 1;
1241 if (!detected_firmware_type && hdr &&
1242 (hdr->n_descsz == 10) &&
1243 (memcmp(n_desc, "LinuxBIOS", 10) == 0)) {
1244 /* Don't believe I'm linuxBIOS unless I can
1245 * find the linuxBIOS table..
1247 detected_firmware_type = find_lb_table(info);
1249 if (!detected_firmware_type && hdr &&
1250 (hdr->n_descsz == 0)) {
1251 /* No firmware is present */
1252 detected_firmware_type = 1;
1254 if (!detected_firmware_type && hdr &&
1255 (hdr->n_descsz == 1) &&
1256 (memcmp(n_desc, "", 1) == 0)) {
1257 /* No firmware is present */
1258 detected_firmware_type = 1;
1260 if (!detected_firmware_type && hdr) {
1261 printf("Unknown firmware type: %s\n", n_desc);
1263 return detected_firmware_type;
1266 static void query_firmware_class(struct param_info *info)
1268 int detected_firmware_type = 0;
1270 /* First say I have no firmware at all... */
1271 info->has_pcbios = 0;
1272 info->has_linuxbios = 0;
1274 /* See if the bootloader has told us what
1275 * kind of firmware we are running on.
1277 detected_firmware_type = bootloader_query_firmware_class(info);
1279 /* See if we can detect linuxbios. */
1280 if (!detected_firmware_type) {
1281 detected_firmware_type = find_lb_table(info);
1284 if (!detected_firmware_type) {
1285 /* if all else fails assume a standard pcbios... */
1286 info->has_pcbios = 1;
1289 /* Now print out the firmware type... */
1290 printf("Firmware type:");
1291 if (info->has_linuxbios) {
1292 printf(" LinuxBIOS");
1294 if (info->has_pcbios) {
1300 static void query_firmware_values(struct param_info *info)
1302 if (info->has_linuxbios) {
1303 query_lb_values(info);
1305 if (info->has_pcbios) {
1306 query_pcbios_values(info);
1313 * =============================================================================
1317 static void print_offsets(void)
1319 struct parameters *real_mode = 0;
1320 printf("print_offsets\n");
1322 printf("orig_x =%x\n", (uint32_t)&real_mode->orig_x);
1323 printf("orig_y =%x\n", (uint32_t)&real_mode->orig_y);
1324 printf("ext_mem_k =%x\n", (uint32_t)&real_mode->ext_mem_k);
1325 printf("orig_video_page =%x\n", (uint32_t)&real_mode->orig_video_page);
1326 printf("orig_video_mode =%x\n", (uint32_t)&real_mode->orig_video_mode);
1327 printf("orig_video_cols =%x\n", (uint32_t)&real_mode->orig_video_cols);
1328 printf("unused2 =%x\n", (uint32_t)&real_mode->unused2);
1329 printf("orig_video_ega_bx =%x\n", (uint32_t)&real_mode->orig_video_ega_bx);
1330 printf("unused3 =%x\n", (uint32_t)&real_mode->unused3);
1331 printf("orig_video_lines =%x\n", (uint32_t)&real_mode->orig_video_lines);
1332 printf("orig_video_isVGA =%x\n", (uint32_t)&real_mode->orig_video_isVGA);
1333 printf("orig_video_points =%x\n", (uint32_t)&real_mode->orig_video_points);
1334 printf("lfb_width =%x\n", (uint32_t)&real_mode->lfb_width);
1335 printf("lfb_height =%x\n", (uint32_t)&real_mode->lfb_height);
1336 printf("lfb_depth =%x\n", (uint32_t)&real_mode->lfb_depth);
1337 printf("lfb_base =%x\n", (uint32_t)&real_mode->lfb_base);
1338 printf("lfb_size =%x\n", (uint32_t)&real_mode->lfb_size);
1339 printf("cl_magic =%x\n", (uint32_t)&real_mode->cl_magic);
1340 printf("cl_offset =%x\n", (uint32_t)&real_mode->cl_offset);
1341 printf("lfb_linelength =%x\n", (uint32_t)&real_mode->lfb_linelength);
1342 printf("red_size =%x\n", (uint32_t)&real_mode->red_size);
1343 printf("red_pos =%x\n", (uint32_t)&real_mode->red_pos);
1344 printf("green_size =%x\n", (uint32_t)&real_mode->green_size);
1345 printf("green_pos =%x\n", (uint32_t)&real_mode->green_pos);
1346 printf("blue_size =%x\n", (uint32_t)&real_mode->blue_size);
1347 printf("blue_pos =%x\n", (uint32_t)&real_mode->blue_pos);
1348 printf("rsvd_size =%x\n", (uint32_t)&real_mode->rsvd_size);
1349 printf("rsvd_pos =%x\n", (uint32_t)&real_mode->rsvd_pos);
1350 printf("vesapm_seg =%x\n", (uint32_t)&real_mode->vesapm_seg);
1351 printf("vesapm_off =%x\n", (uint32_t)&real_mode->vesapm_off);
1352 printf("pages =%x\n", (uint32_t)&real_mode->pages);
1353 printf("reserved4 =%x\n", (uint32_t)&real_mode->reserved4);
1354 printf("apm_bios_info =%x\n", (uint32_t)&real_mode->apm_bios_info);
1355 printf("drive_info =%x\n", (uint32_t)&real_mode->drive_info);
1356 printf("sys_desc_table =%x\n", (uint32_t)&real_mode->sys_desc_table);
1357 printf("alt_mem_k =%x\n", (uint32_t)&real_mode->alt_mem_k);
1358 printf("reserved5 =%x\n", (uint32_t)&real_mode->reserved5);
1359 printf("e820_map_nr =%x\n", (uint32_t)&real_mode->e820_map_nr);
1360 printf("reserved6 =%x\n", (uint32_t)&real_mode->reserved6);
1361 printf("mount_root_rdonly =%x\n", (uint32_t)&real_mode->mount_root_rdonly);
1362 printf("reserved7 =%x\n", (uint32_t)&real_mode->reserved7);
1363 printf("ramdisk_flags =%x\n", (uint32_t)&real_mode->ramdisk_flags);
1364 printf("reserved8 =%x\n", (uint32_t)&real_mode->reserved8);
1365 printf("orig_root_dev =%x\n", (uint32_t)&real_mode->orig_root_dev);
1366 printf("reserved9 =%x\n", (uint32_t)&real_mode->reserved9);
1367 printf("aux_device_info =%x\n", (uint32_t)&real_mode->aux_device_info);
1368 printf("reserved10 =%x\n", (uint32_t)&real_mode->reserved10);
1369 printf("param_block_signature=%x\n", (uint32_t)&real_mode->param_block_signature);
1370 printf("param_block_version =%x\n", (uint32_t)&real_mode->param_block_version);
1371 printf("reserved11 =%x\n", (uint32_t)&real_mode->reserved11);
1372 printf("loader_type =%x\n", (uint32_t)&real_mode->loader_type);
1373 printf("loader_flags =%x\n", (uint32_t)&real_mode->loader_flags);
1374 printf("reserved12 =%x\n", (uint32_t)&real_mode->reserved12);
1375 printf("kernel_start =%x\n", (uint32_t)&real_mode->kernel_start);
1376 printf("initrd_start =%x\n", (uint32_t)&real_mode->initrd_start);
1377 printf("initrd_size =%x\n", (uint32_t)&real_mode->initrd_size);
1378 printf("reserved13 =%x\n", (uint32_t)&real_mode->reserved13);
1379 printf("e820_map =%x\n", (uint32_t)&real_mode->e820_map);
1380 printf("reserved16 =%x\n", (uint32_t)&real_mode->reserved16);
1381 printf("command_line =%x\n", (uint32_t)&real_mode->command_line);
1382 printf("reserved17 =%x\n", (uint32_t)&real_mode->reserved17);
1385 static void print_linux_params(struct param_info *info)
1389 printf("print_linux_params\n");
1390 /* Default screen size */
1391 printf("orig_x =%x\n", info->real_mode->orig_x);
1392 printf("orig_y =%x\n", info->real_mode->orig_y);
1393 printf("orig_video_page =%x\n", info->real_mode->orig_video_page);
1394 printf("orig_video_mode =%x\n", info->real_mode->orig_video_mode);
1395 printf("orig_video_cols =%x\n", info->real_mode->orig_video_cols);
1396 printf("orig_video_lines =%x\n", info->real_mode->orig_video_lines);
1397 printf("orig_video_ega_bx=%x\n", info->real_mode->orig_video_ega_bx);
1398 printf("orig_video_isVGA =%x\n", info->real_mode->orig_video_isVGA);
1399 printf("orig_video_points=%x\n", info->real_mode->orig_video_points);
1402 /* System descriptor table... */
1403 printf("sys_dest_table_len=%x\n", info->real_mode->sys_desc_table.length);
1406 printf("ext_mem_k =%x\n", info->real_mode->ext_mem_k);
1407 printf("alt_mem_k =%x\n", info->real_mode->alt_mem_k);
1408 printf("e820_map_nr =%x\n", info->real_mode->e820_map_nr);
1409 for(i = 0; i < E820MAX; i++) {
1410 printf("addr[%x] =%Lx\n",
1411 i, info->real_mode->e820_map[i].addr);
1412 printf("size[%x] =%Lx\n",
1413 i, info->real_mode->e820_map[i].size);
1414 printf("type[%x] =%Lx\n",
1415 i, info->real_mode->e820_map[i].type);
1417 printf("mount_root_rdonly=%x\n", info->real_mode->mount_root_rdonly);
1418 printf("ramdisk_flags =%x\n", info->real_mode->ramdisk_flags);
1419 printf("orig_root_dev =%x\n", info->real_mode->orig_root_dev);
1420 printf("aux_device_info =%x\n", info->real_mode->aux_device_info);
1421 printf("param_block_signature=%x\n", *((uint32_t *)info->real_mode->param_block_signature));
1422 printf("loader_type =%x\n", info->real_mode->loader_type);
1423 printf("loader_flags =%x\n", info->real_mode->loader_flags);
1424 printf("initrd_start =%x\n", info->real_mode->initrd_start);
1425 printf("initrd_size =%x\n", info->real_mode->initrd_size);
1427 /* Where I'm putting the command line */
1428 printf("cl_magic =%x\n", info->real_mode->cl_magic);
1429 printf("cl_offset =%x\n", info->real_mode->cl_offset);
1431 /* Now print the command line */
1432 printf("command_line =%s\n", info->real_mode->command_line);
1440 * =============================================================================
1443 void initialize_linux_params(struct param_info *info)
1446 /* First the defaults */
1447 memset(info->real_mode, 0, PAGE_SIZE);
1449 /* Default screen size */
1450 info->real_mode->orig_x = 0;
1451 info->real_mode->orig_y = 25;
1452 info->real_mode->orig_video_page = 0;
1453 info->real_mode->orig_video_mode = 0;
1454 info->real_mode->orig_video_cols = 80;
1455 info->real_mode->orig_video_lines = 25;
1456 info->real_mode->orig_video_ega_bx = 0;
1457 info->real_mode->orig_video_isVGA = 1;
1458 info->real_mode->orig_video_points = 16;
1460 /* Fill this in later */
1461 info->real_mode->ext_mem_k = 0;
1463 /* Fill in later... */
1464 info->real_mode->e820_map_nr = 0;
1466 /* Where I'm putting the command line */
1467 info->real_mode->cl_magic = CL_MAGIC_VALUE;
1468 info->real_mode->cl_offset = 2048;
1470 info->real_mode->cmd_line_ptr = info->real_mode->cl_offset + (unsigned long) info->real_mode;
1472 /* Now set the command line */
1473 len = strnlen(info->image->cmdline, sizeof(info->real_mode->command_line) -1);
1474 memcpy(info->real_mode->command_line, info->image->cmdline, len);
1475 info->real_mode->command_line[len] = '\0';
1477 /* from the bios initially */
1478 memset(&info->real_mode->apm_bios_info, 0, sizeof(info->real_mode->apm_bios_info));
1480 memset(&info->real_mode->drive_info, 0, sizeof(info->real_mode->drive_info));
1482 /* forget it for now... */
1483 info->real_mode->sys_desc_table.length = 0;
1485 /* Fill this in later */
1486 info->real_mode->alt_mem_k = 0;
1487 info->real_mode->ext_mem_k = 0;
1489 /* default yes: this can be overridden on the command line */
1490 info->real_mode->mount_root_rdonly = 0xFFFF;
1492 /* old ramdisk options, These really should be command line
1495 info->real_mode->ramdisk_flags = info->image->ramdisk_flags;
1497 /* default to /dev/hda.
1498 * Override this on the command line if necessary
1500 info->real_mode->orig_root_dev = info->image->root_dev;
1502 /* Originally from the bios? */
1503 info->real_mode->aux_device_info = 0;
1505 /* Boot block magic */
1506 memcpy(info->real_mode->param_block_signature, "HdrS", 4);
1507 info->real_mode->param_block_version = 0x0201;
1509 /* Say I'm a kernel boot loader */
1510 info->real_mode->loader_type = (LOADER_TYPE_KERNEL << 4) + 0 /* version */;
1512 /* No loader flags */
1513 info->real_mode->loader_flags = 0;
1515 /* Ramdisk address and size ... */
1516 info->real_mode->initrd_start = 0;
1517 info->real_mode->initrd_size = 0;
1518 if (info->image->initrd_size) {
1519 info->real_mode->initrd_start = info->image->initrd_start;
1520 info->real_mode->initrd_size = info->image->initrd_size;
1523 /* Now remember those things that I need */
1524 info->need_mem_sizes = 1;
1527 void *convert_params(unsigned type, void *data, void *param, void *image)
1529 struct param_info info;
1531 printf("hello world\n");
1533 info.real_mode = faked_real_mode;
1538 initialize_linux_params(&info);
1539 query_bootloader_param_class(&info);
1540 query_firmware_class(&info);
1541 query_firmware_values(&info);
1542 query_bootloader_values(&info);
1544 /* Do the hardware setup that linux might forget... */
1545 hardware_setup(&info);
1547 /* Print some debugging information */
1549 printf("EXT_MEM_K=%x\n", info.real_mode->ext_mem_k);
1550 printf("ALT_MEM_K=%x\n", info.real_mode->alt_mem_k);
1554 print_linux_params(&info);
1557 printf("info.real_mode = 0x%x\n", info.real_mode );
1558 printf("Jumping to Linux\n");
1560 return info.real_mode;