bootpz.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * arch/alpha/boot/bootpz.c
  3. *
  4. * Copyright (C) 1997 Jay Estabrook
  5. *
  6. * This file is used for creating a compressed BOOTP file for the
  7. * Linux/AXP kernel
  8. *
  9. * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
  10. * and the decompression code from MILO.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <generated/utsrelease.h>
  16. #include <linux/mm.h>
  17. #include <asm/console.h>
  18. #include <asm/hwrpb.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/io.h>
  21. #include <stdarg.h>
  22. #include "kzsize.h"
  23. /* FIXME FIXME FIXME */
  24. #define MALLOC_AREA_SIZE 0x200000 /* 2MB for now */
  25. /* FIXME FIXME FIXME */
  26. /*
  27. WARNING NOTE
  28. It is very possible that turning on additional messages may cause
  29. kernel image corruption due to stack usage to do the printing.
  30. */
  31. #undef DEBUG_CHECK_RANGE
  32. #undef DEBUG_ADDRESSES
  33. #undef DEBUG_LAST_STEPS
  34. extern unsigned long switch_to_osf_pal(unsigned long nr,
  35. struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
  36. unsigned long *vptb);
  37. extern int decompress_kernel(void* destination, void *source,
  38. size_t ksize, size_t kzsize);
  39. extern void move_stack(unsigned long new_stack);
  40. struct hwrpb_struct *hwrpb = INIT_HWRPB;
  41. static struct pcb_struct pcb_va[1];
  42. /*
  43. * Find a physical address of a virtual object..
  44. *
  45. * This is easy using the virtual page table address.
  46. */
  47. #define VPTB ((unsigned long *) 0x200000000)
  48. static inline unsigned long
  49. find_pa(unsigned long address)
  50. {
  51. unsigned long result;
  52. result = VPTB[address >> 13];
  53. result >>= 32;
  54. result <<= 13;
  55. result |= address & 0x1fff;
  56. return result;
  57. }
  58. int
  59. check_range(unsigned long vstart, unsigned long vend,
  60. unsigned long kstart, unsigned long kend)
  61. {
  62. unsigned long vaddr, kaddr;
  63. #ifdef DEBUG_CHECK_RANGE
  64. srm_printk("check_range: V[0x%lx:0x%lx] K[0x%lx:0x%lx]\n",
  65. vstart, vend, kstart, kend);
  66. #endif
  67. /* do some range checking for detecting an overlap... */
  68. for (vaddr = vstart; vaddr <= vend; vaddr += PAGE_SIZE)
  69. {
  70. kaddr = (find_pa(vaddr) | PAGE_OFFSET);
  71. if (kaddr >= kstart && kaddr <= kend)
  72. {
  73. #ifdef DEBUG_CHECK_RANGE
  74. srm_printk("OVERLAP: vaddr 0x%lx kaddr 0x%lx"
  75. " [0x%lx:0x%lx]\n",
  76. vaddr, kaddr, kstart, kend);
  77. #endif
  78. return 1;
  79. }
  80. }
  81. return 0;
  82. }
  83. /*
  84. * This function moves into OSF/1 pal-code, and has a temporary
  85. * PCB for that. The kernel proper should replace this PCB with
  86. * the real one as soon as possible.
  87. *
  88. * The page table muckery in here depends on the fact that the boot
  89. * code has the L1 page table identity-map itself in the second PTE
  90. * in the L1 page table. Thus the L1-page is virtually addressable
  91. * itself (through three levels) at virtual address 0x200802000.
  92. */
  93. #define L1 ((unsigned long *) 0x200802000)
  94. void
  95. pal_init(void)
  96. {
  97. unsigned long i, rev;
  98. struct percpu_struct * percpu;
  99. struct pcb_struct * pcb_pa;
  100. /* Create the dummy PCB. */
  101. pcb_va->ksp = 0;
  102. pcb_va->usp = 0;
  103. pcb_va->ptbr = L1[1] >> 32;
  104. pcb_va->asn = 0;
  105. pcb_va->pcc = 0;
  106. pcb_va->unique = 0;
  107. pcb_va->flags = 1;
  108. pcb_va->res1 = 0;
  109. pcb_va->res2 = 0;
  110. pcb_pa = (struct pcb_struct *)find_pa((unsigned long)pcb_va);
  111. /*
  112. * a0 = 2 (OSF)
  113. * a1 = return address, but we give the asm the vaddr of the PCB
  114. * a2 = physical addr of PCB
  115. * a3 = new virtual page table pointer
  116. * a4 = KSP (but the asm sets it)
  117. */
  118. srm_printk("Switching to OSF PAL-code... ");
  119. i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
  120. if (i) {
  121. srm_printk("failed, code %ld\n", i);
  122. __halt();
  123. }
  124. percpu = (struct percpu_struct *)
  125. (INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
  126. rev = percpu->pal_revision = percpu->palcode_avail[2];
  127. srm_printk("OK (rev %lx)\n", rev);
  128. tbia(); /* do it directly in case we are SMP */
  129. }
  130. /*
  131. * Start the kernel.
  132. */
  133. static inline void
  134. runkernel(void)
  135. {
  136. __asm__ __volatile__(
  137. "bis %0,%0,$27\n\t"
  138. "jmp ($27)"
  139. : /* no outputs: it doesn't even return */
  140. : "r" (START_ADDR));
  141. }
  142. /* Must record the SP (it is virtual) on entry, so we can make sure
  143. not to overwrite it during movement or decompression. */
  144. unsigned long SP_on_entry;
  145. /* Calculate the kernel image address based on the end of the BOOTP
  146. bootstrapper (ie this program).
  147. */
  148. extern char _end;
  149. #define KERNEL_ORIGIN \
  150. ((((unsigned long)&_end) + 511) & ~511)
  151. /* Round address to next higher page boundary. */
  152. #define NEXT_PAGE(a) (((a) | (PAGE_SIZE - 1)) + 1)
  153. #ifdef INITRD_IMAGE_SIZE
  154. # define REAL_INITRD_SIZE INITRD_IMAGE_SIZE
  155. #else
  156. # define REAL_INITRD_SIZE 0
  157. #endif
  158. /* Defines from include/asm-alpha/system.h
  159. BOOT_ADDR Virtual address at which the consoles loads
  160. the BOOTP image.
  161. KERNEL_START KSEG address at which the kernel is built to run,
  162. which includes some initial data pages before the
  163. code.
  164. START_ADDR KSEG address of the entry point of kernel code.
  165. ZERO_PGE KSEG address of page full of zeroes, but
  166. upon entry to kerne cvan be expected
  167. to hold the parameter list and possible
  168. INTRD information.
  169. These are used in the local defines below.
  170. */
  171. /* Virtual addresses for the BOOTP image. Note that this includes the
  172. bootstrapper code as well as the compressed kernel image, and
  173. possibly the INITRD image.
  174. Oh, and do NOT forget the STACK, which appears to be placed virtually
  175. beyond the end of the loaded image.
  176. */
  177. #define V_BOOT_IMAGE_START BOOT_ADDR
  178. #define V_BOOT_IMAGE_END SP_on_entry
  179. /* Virtual addresses for just the bootstrapper part of the BOOTP image. */
  180. #define V_BOOTSTRAPPER_START BOOT_ADDR
  181. #define V_BOOTSTRAPPER_END KERNEL_ORIGIN
  182. /* Virtual addresses for just the data part of the BOOTP
  183. image. This may also include the INITRD image, but always
  184. includes the STACK.
  185. */
  186. #define V_DATA_START KERNEL_ORIGIN
  187. #define V_INITRD_START (KERNEL_ORIGIN + KERNEL_Z_SIZE)
  188. #define V_INTRD_END (V_INITRD_START + REAL_INITRD_SIZE)
  189. #define V_DATA_END V_BOOT_IMAGE_END
  190. /* KSEG addresses for the uncompressed kernel.
  191. Note that the end address includes workspace for the decompression.
  192. Note also that the DATA_START address is ZERO_PGE, to which we write
  193. just before jumping to the kernel image at START_ADDR.
  194. */
  195. #define K_KERNEL_DATA_START ZERO_PGE
  196. #define K_KERNEL_IMAGE_START START_ADDR
  197. #define K_KERNEL_IMAGE_END (START_ADDR + KERNEL_SIZE)
  198. /* Define to where we may have to decompress the kernel image, before
  199. we move it to the final position, in case of overlap. This will be
  200. above the final position of the kernel.
  201. Regardless of overlap, we move the INITRD image to the end of this
  202. copy area, because there needs to be a buffer area after the kernel
  203. for "bootmem" anyway.
  204. */
  205. #define K_COPY_IMAGE_START NEXT_PAGE(K_KERNEL_IMAGE_END)
  206. /* Reserve one page below INITRD for the new stack. */
  207. #define K_INITRD_START \
  208. NEXT_PAGE(K_COPY_IMAGE_START + KERNEL_SIZE + PAGE_SIZE)
  209. #define K_COPY_IMAGE_END \
  210. (K_INITRD_START + REAL_INITRD_SIZE + MALLOC_AREA_SIZE)
  211. #define K_COPY_IMAGE_SIZE \
  212. NEXT_PAGE(K_COPY_IMAGE_END - K_COPY_IMAGE_START)
  213. void
  214. start_kernel(void)
  215. {
  216. int must_move = 0;
  217. /* Initialize these for the decompression-in-place situation,
  218. which is the smallest amount of work and most likely to
  219. occur when using the normal START_ADDR of the kernel
  220. (currently set to 16MB, to clear all console code.
  221. */
  222. unsigned long uncompressed_image_start = K_KERNEL_IMAGE_START;
  223. unsigned long uncompressed_image_end = K_KERNEL_IMAGE_END;
  224. unsigned long initrd_image_start = K_INITRD_START;
  225. /*
  226. * Note that this crufty stuff with static and envval
  227. * and envbuf is because:
  228. *
  229. * 1. Frequently, the stack is short, and we don't want to overrun;
  230. * 2. Frequently the stack is where we are going to copy the kernel to;
  231. * 3. A certain SRM console required the GET_ENV output to stack.
  232. * ??? A comment in the aboot sources indicates that the GET_ENV
  233. * destination must be quadword aligned. Might this explain the
  234. * behaviour, rather than requiring output to the stack, which
  235. * seems rather far-fetched.
  236. */
  237. static long nbytes;
  238. static char envval[256] __attribute__((aligned(8)));
  239. register unsigned long asm_sp asm("30");
  240. SP_on_entry = asm_sp;
  241. srm_printk("Linux/Alpha BOOTPZ Loader for Linux " UTS_RELEASE "\n");
  242. /* Validity check the HWRPB. */
  243. if (INIT_HWRPB->pagesize != 8192) {
  244. srm_printk("Expected 8kB pages, got %ldkB\n",
  245. INIT_HWRPB->pagesize >> 10);
  246. return;
  247. }
  248. if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
  249. srm_printk("Expected vptb at %p, got %p\n",
  250. VPTB, (void *)INIT_HWRPB->vptb);
  251. return;
  252. }
  253. /* PALcode (re)initialization. */
  254. pal_init();
  255. /* Get the parameter list from the console environment variable. */
  256. nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
  257. if (nbytes < 0 || nbytes >= sizeof(envval)) {
  258. nbytes = 0;
  259. }
  260. envval[nbytes] = '\0';
  261. #ifdef DEBUG_ADDRESSES
  262. srm_printk("START_ADDR 0x%lx\n", START_ADDR);
  263. srm_printk("KERNEL_ORIGIN 0x%lx\n", KERNEL_ORIGIN);
  264. srm_printk("KERNEL_SIZE 0x%x\n", KERNEL_SIZE);
  265. srm_printk("KERNEL_Z_SIZE 0x%x\n", KERNEL_Z_SIZE);
  266. #endif
  267. /* Since all the SRM consoles load the BOOTP image at virtual
  268. * 0x20000000, we have to ensure that the physical memory
  269. * pages occupied by that image do NOT overlap the physical
  270. * address range where the kernel wants to be run. This
  271. * causes real problems when attempting to cdecompress the
  272. * former into the latter... :-(
  273. *
  274. * So, we may have to decompress/move the kernel/INITRD image
  275. * virtual-to-physical someplace else first before moving
  276. * kernel /INITRD to their final resting places... ;-}
  277. *
  278. * Sigh...
  279. */
  280. /* First, check to see if the range of addresses occupied by
  281. the bootstrapper part of the BOOTP image include any of the
  282. physical pages into which the kernel will be placed for
  283. execution.
  284. We only need check on the final kernel image range, since we
  285. will put the INITRD someplace that we can be sure is not
  286. in conflict.
  287. */
  288. if (check_range(V_BOOTSTRAPPER_START, V_BOOTSTRAPPER_END,
  289. K_KERNEL_DATA_START, K_KERNEL_IMAGE_END))
  290. {
  291. srm_printk("FATAL ERROR: overlap of bootstrapper code\n");
  292. __halt();
  293. }
  294. /* Next, check to see if the range of addresses occupied by
  295. the compressed kernel/INITRD/stack portion of the BOOTP
  296. image include any of the physical pages into which the
  297. decompressed kernel or the INITRD will be placed for
  298. execution.
  299. */
  300. if (check_range(V_DATA_START, V_DATA_END,
  301. K_KERNEL_IMAGE_START, K_COPY_IMAGE_END))
  302. {
  303. #ifdef DEBUG_ADDRESSES
  304. srm_printk("OVERLAP: cannot decompress in place\n");
  305. #endif
  306. uncompressed_image_start = K_COPY_IMAGE_START;
  307. uncompressed_image_end = K_COPY_IMAGE_END;
  308. must_move = 1;
  309. /* Finally, check to see if the range of addresses
  310. occupied by the compressed kernel/INITRD part of
  311. the BOOTP image include any of the physical pages
  312. into which that part is to be copied for
  313. decompression.
  314. */
  315. while (check_range(V_DATA_START, V_DATA_END,
  316. uncompressed_image_start,
  317. uncompressed_image_end))
  318. {
  319. #if 0
  320. uncompressed_image_start += K_COPY_IMAGE_SIZE;
  321. uncompressed_image_end += K_COPY_IMAGE_SIZE;
  322. initrd_image_start += K_COPY_IMAGE_SIZE;
  323. #else
  324. /* Keep as close as possible to end of BOOTP image. */
  325. uncompressed_image_start += PAGE_SIZE;
  326. uncompressed_image_end += PAGE_SIZE;
  327. initrd_image_start += PAGE_SIZE;
  328. #endif
  329. }
  330. }
  331. srm_printk("Starting to load the kernel with args '%s'\n", envval);
  332. #ifdef DEBUG_ADDRESSES
  333. srm_printk("Decompressing the kernel...\n"
  334. "...from 0x%lx to 0x%lx size 0x%x\n",
  335. V_DATA_START,
  336. uncompressed_image_start,
  337. KERNEL_SIZE);
  338. #endif
  339. decompress_kernel((void *)uncompressed_image_start,
  340. (void *)V_DATA_START,
  341. KERNEL_SIZE, KERNEL_Z_SIZE);
  342. /*
  343. * Now, move things to their final positions, if/as required.
  344. */
  345. #ifdef INITRD_IMAGE_SIZE
  346. /* First, we always move the INITRD image, if present. */
  347. #ifdef DEBUG_ADDRESSES
  348. srm_printk("Moving the INITRD image...\n"
  349. " from 0x%lx to 0x%lx size 0x%x\n",
  350. V_INITRD_START,
  351. initrd_image_start,
  352. INITRD_IMAGE_SIZE);
  353. #endif
  354. memcpy((void *)initrd_image_start, (void *)V_INITRD_START,
  355. INITRD_IMAGE_SIZE);
  356. #endif /* INITRD_IMAGE_SIZE */
  357. /* Next, we may have to move the uncompressed kernel to the
  358. final destination.
  359. */
  360. if (must_move) {
  361. #ifdef DEBUG_ADDRESSES
  362. srm_printk("Moving the uncompressed kernel...\n"
  363. "...from 0x%lx to 0x%lx size 0x%x\n",
  364. uncompressed_image_start,
  365. K_KERNEL_IMAGE_START,
  366. (unsigned)KERNEL_SIZE);
  367. #endif
  368. /*
  369. * Move the stack to a safe place to ensure it won't be
  370. * overwritten by kernel image.
  371. */
  372. move_stack(initrd_image_start - PAGE_SIZE);
  373. memcpy((void *)K_KERNEL_IMAGE_START,
  374. (void *)uncompressed_image_start, KERNEL_SIZE);
  375. }
  376. /* Clear the zero page, then move the argument list in. */
  377. #ifdef DEBUG_LAST_STEPS
  378. srm_printk("Preparing ZERO_PGE...\n");
  379. #endif
  380. memset((char*)ZERO_PGE, 0, PAGE_SIZE);
  381. strcpy((char*)ZERO_PGE, envval);
  382. #ifdef INITRD_IMAGE_SIZE
  383. #ifdef DEBUG_LAST_STEPS
  384. srm_printk("Preparing INITRD info...\n");
  385. #endif
  386. /* Finally, set the INITRD paramenters for the kernel. */
  387. ((long *)(ZERO_PGE+256))[0] = initrd_image_start;
  388. ((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;
  389. #endif /* INITRD_IMAGE_SIZE */
  390. #ifdef DEBUG_LAST_STEPS
  391. srm_printk("Doing 'runkernel()'...\n");
  392. #endif
  393. runkernel();
  394. }
  395. /* dummy function, should never be called. */
  396. void *__kmalloc(size_t size, gfp_t flags)
  397. {
  398. return (void *)NULL;
  399. }