main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. *
  4. * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <stdarg.h>
  12. #include <stddef.h>
  13. #include "elf.h"
  14. #include "page.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. #include "ops.h"
  18. #include "gunzip_util.h"
  19. #include "flatdevtree.h"
  20. #include "reg.h"
  21. static struct gunzip_state gzstate;
  22. struct addr_range {
  23. void *addr;
  24. unsigned long size;
  25. };
  26. #undef DEBUG
  27. static struct addr_range prep_kernel(void)
  28. {
  29. char elfheader[256];
  30. void *vmlinuz_addr = _vmlinux_start;
  31. unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
  32. void *addr = 0;
  33. struct elf_info ei;
  34. int len;
  35. /* gunzip the ELF header of the kernel */
  36. gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
  37. gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
  38. if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
  39. fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
  40. if (platform_ops.image_hdr)
  41. platform_ops.image_hdr(elfheader);
  42. /* We need to alloc the memsize: gzip will expand the kernel
  43. * text/data, then possible rubbish we don't care about. But
  44. * the kernel bss must be claimed (it will be zero'd by the
  45. * kernel itself)
  46. */
  47. printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize);
  48. if (platform_ops.vmlinux_alloc) {
  49. addr = platform_ops.vmlinux_alloc(ei.memsize);
  50. } else {
  51. if ((unsigned long)_start < ei.memsize)
  52. fatal("Insufficient memory for kernel at address 0!"
  53. " (_start=%p)\n\r", _start);
  54. }
  55. /* Finally, gunzip the kernel */
  56. printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr,
  57. vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
  58. /* discard up to the actual load data */
  59. gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader));
  60. len = gunzip_finish(&gzstate, addr, ei.loadsize);
  61. if (len != ei.loadsize)
  62. fatal("ran out of data! only got 0x%x of 0x%lx bytes.\n\r",
  63. len, ei.loadsize);
  64. printf("done 0x%x bytes\n\r", len);
  65. flush_cache(addr, ei.loadsize);
  66. return (struct addr_range){addr, ei.memsize};
  67. }
  68. static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
  69. unsigned long initrd_addr,
  70. unsigned long initrd_size)
  71. {
  72. /* If we have an image attached to us, it overrides anything
  73. * supplied by the loader. */
  74. if (_initrd_end > _initrd_start) {
  75. printf("Attached initrd image at 0x%p-0x%p\n\r",
  76. _initrd_start, _initrd_end);
  77. initrd_addr = (unsigned long)_initrd_start;
  78. initrd_size = _initrd_end - _initrd_start;
  79. } else if (initrd_size > 0) {
  80. printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
  81. initrd_addr, initrd_addr + initrd_size);
  82. }
  83. /* If there's no initrd at all, we're done */
  84. if (! initrd_size)
  85. return (struct addr_range){0, 0};
  86. /*
  87. * If the initrd is too low it will be clobbered when the
  88. * kernel relocates to its final location. In this case,
  89. * allocate a safer place and move it.
  90. */
  91. if (initrd_addr < vmlinux.size) {
  92. void *old_addr = (void *)initrd_addr;
  93. printf("Allocating 0x%lx bytes for initrd ...\n\r",
  94. initrd_size);
  95. initrd_addr = (unsigned long)malloc(initrd_size);
  96. if (! initrd_addr)
  97. fatal("Can't allocate memory for initial "
  98. "ramdisk !\n\r");
  99. printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r",
  100. initrd_addr, old_addr, initrd_size);
  101. memmove((void *)initrd_addr, old_addr, initrd_size);
  102. }
  103. printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
  104. /* Tell the kernel initrd address via device tree */
  105. setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
  106. setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
  107. return (struct addr_range){(void *)initrd_addr, initrd_size};
  108. }
  109. /* A buffer that may be edited by tools operating on a zImage binary so as to
  110. * edit the command line passed to vmlinux (by setting /chosen/bootargs).
  111. * The buffer is put in it's own section so that tools may locate it easier.
  112. */
  113. static char cmdline[COMMAND_LINE_SIZE]
  114. __attribute__((__section__("__builtin_cmdline")));
  115. static void prep_cmdline(void *chosen)
  116. {
  117. if (cmdline[0] == '\0')
  118. getprop(chosen, "bootargs", cmdline, COMMAND_LINE_SIZE-1);
  119. printf("\n\rLinux/PowerPC load: %s", cmdline);
  120. /* If possible, edit the command line */
  121. if (console_ops.edit_cmdline)
  122. console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
  123. printf("\n\r");
  124. /* Put the command line back into the devtree for the kernel */
  125. setprop_str(chosen, "bootargs", cmdline);
  126. }
  127. struct platform_ops platform_ops;
  128. struct dt_ops dt_ops;
  129. struct console_ops console_ops;
  130. struct loader_info loader_info;
  131. void start(void)
  132. {
  133. struct addr_range vmlinux, initrd;
  134. kernel_entry_t kentry;
  135. unsigned long ft_addr = 0;
  136. void *chosen;
  137. /* Do this first, because malloc() could clobber the loader's
  138. * command line. Only use the loader command line if a
  139. * built-in command line wasn't set by an external tool */
  140. if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
  141. memmove(cmdline, loader_info.cmdline,
  142. min(loader_info.cmdline_len, COMMAND_LINE_SIZE-1));
  143. if (console_ops.open && (console_ops.open() < 0))
  144. exit();
  145. if (platform_ops.fixups)
  146. platform_ops.fixups();
  147. printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
  148. _start, get_sp());
  149. /* Ensure that the device tree has a /chosen node */
  150. chosen = finddevice("/chosen");
  151. if (!chosen)
  152. chosen = create_node(NULL, "chosen");
  153. vmlinux = prep_kernel();
  154. initrd = prep_initrd(vmlinux, chosen,
  155. loader_info.initrd_addr, loader_info.initrd_size);
  156. prep_cmdline(chosen);
  157. printf("Finalizing device tree...");
  158. if (dt_ops.finalize)
  159. ft_addr = dt_ops.finalize();
  160. if (ft_addr)
  161. printf(" flat tree at 0x%lx\n\r", ft_addr);
  162. else
  163. printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
  164. if (console_ops.close)
  165. console_ops.close();
  166. kentry = (kernel_entry_t) vmlinux.addr;
  167. if (ft_addr)
  168. kentry(ft_addr, 0, NULL);
  169. else
  170. kentry((unsigned long)initrd.addr, initrd.size,
  171. loader_info.promptr);
  172. /* console closed so printf in fatal below may not work */
  173. fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
  174. }