do_mounts_rd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <linux/kernel.h>
  2. #include <linux/fs.h>
  3. #include <linux/minix_fs.h>
  4. #include <linux/ext2_fs.h>
  5. #include <linux/romfs_fs.h>
  6. #include <linux/cramfs_fs.h>
  7. #include <linux/initrd.h>
  8. #include <linux/string.h>
  9. #include "do_mounts.h"
  10. #include "../fs/squashfs/squashfs_fs.h"
  11. int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
  12. static int __init prompt_ramdisk(char *str)
  13. {
  14. rd_prompt = simple_strtol(str,NULL,0) & 1;
  15. return 1;
  16. }
  17. __setup("prompt_ramdisk=", prompt_ramdisk);
  18. int __initdata rd_image_start; /* starting block # of image */
  19. static int __init ramdisk_start_setup(char *str)
  20. {
  21. rd_image_start = simple_strtol(str,NULL,0);
  22. return 1;
  23. }
  24. __setup("ramdisk_start=", ramdisk_start_setup);
  25. static int __init crd_load(int in_fd, int out_fd);
  26. /*
  27. * This routine tries to find a RAM disk image to load, and returns the
  28. * number of blocks to read for a non-compressed image, 0 if the image
  29. * is a compressed image, and -1 if an image with the right magic
  30. * numbers could not be found.
  31. *
  32. * We currently check for the following magic numbers:
  33. * minix
  34. * ext2
  35. * romfs
  36. * cramfs
  37. * squashfs
  38. * gzip
  39. */
  40. static int __init
  41. identify_ramdisk_image(int fd, int start_block)
  42. {
  43. const int size = 512;
  44. struct minix_super_block *minixsb;
  45. struct ext2_super_block *ext2sb;
  46. struct romfs_super_block *romfsb;
  47. struct cramfs_super *cramfsb;
  48. struct squashfs_super_block *squashfsb;
  49. int nblocks = -1;
  50. unsigned char *buf;
  51. buf = kmalloc(size, GFP_KERNEL);
  52. if (!buf)
  53. return -1;
  54. minixsb = (struct minix_super_block *) buf;
  55. ext2sb = (struct ext2_super_block *) buf;
  56. romfsb = (struct romfs_super_block *) buf;
  57. cramfsb = (struct cramfs_super *) buf;
  58. squashfsb = (struct squashfs_super_block *) buf;
  59. memset(buf, 0xe5, size);
  60. /*
  61. * Read block 0 to test for gzipped kernel
  62. */
  63. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  64. sys_read(fd, buf, size);
  65. /*
  66. * If it matches the gzip magic numbers, return 0
  67. */
  68. if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
  69. printk(KERN_NOTICE
  70. "RAMDISK: Compressed image found at block %d\n",
  71. start_block);
  72. nblocks = 0;
  73. goto done;
  74. }
  75. /* romfs is at block zero too */
  76. if (romfsb->word0 == ROMSB_WORD0 &&
  77. romfsb->word1 == ROMSB_WORD1) {
  78. printk(KERN_NOTICE
  79. "RAMDISK: romfs filesystem found at block %d\n",
  80. start_block);
  81. nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  82. goto done;
  83. }
  84. if (cramfsb->magic == CRAMFS_MAGIC) {
  85. printk(KERN_NOTICE
  86. "RAMDISK: cramfs filesystem found at block %d\n",
  87. start_block);
  88. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  89. goto done;
  90. }
  91. /* squashfs is at block zero too */
  92. if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
  93. printk(KERN_NOTICE
  94. "RAMDISK: squashfs filesystem found at block %d\n",
  95. start_block);
  96. nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
  97. >> BLOCK_SIZE_BITS;
  98. goto done;
  99. }
  100. /*
  101. * Read block 1 to test for minix and ext2 superblock
  102. */
  103. sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
  104. sys_read(fd, buf, size);
  105. /* Try minix */
  106. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  107. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  108. printk(KERN_NOTICE
  109. "RAMDISK: Minix filesystem found at block %d\n",
  110. start_block);
  111. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  112. goto done;
  113. }
  114. /* Try ext2 */
  115. if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
  116. printk(KERN_NOTICE
  117. "RAMDISK: ext2 filesystem found at block %d\n",
  118. start_block);
  119. nblocks = le32_to_cpu(ext2sb->s_blocks_count) <<
  120. le32_to_cpu(ext2sb->s_log_block_size);
  121. goto done;
  122. }
  123. printk(KERN_NOTICE
  124. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  125. start_block);
  126. done:
  127. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  128. kfree(buf);
  129. return nblocks;
  130. }
  131. int __init rd_load_image(char *from)
  132. {
  133. int res = 0;
  134. int in_fd, out_fd;
  135. unsigned long rd_blocks, devblocks;
  136. int nblocks, i, disk;
  137. char *buf = NULL;
  138. unsigned short rotate = 0;
  139. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  140. char rotator[4] = { '|' , '/' , '-' , '\\' };
  141. #endif
  142. out_fd = sys_open("/dev/ram", O_RDWR, 0);
  143. if (out_fd < 0)
  144. goto out;
  145. in_fd = sys_open(from, O_RDONLY, 0);
  146. if (in_fd < 0)
  147. goto noclose_input;
  148. nblocks = identify_ramdisk_image(in_fd, rd_image_start);
  149. if (nblocks < 0)
  150. goto done;
  151. if (nblocks == 0) {
  152. if (crd_load(in_fd, out_fd) == 0)
  153. goto successful_load;
  154. goto done;
  155. }
  156. /*
  157. * NOTE NOTE: nblocks is not actually blocks but
  158. * the number of kibibytes of data to load into a ramdisk.
  159. * So any ramdisk block size that is a multiple of 1KiB should
  160. * work when the appropriate ramdisk_blocksize is specified
  161. * on the command line.
  162. *
  163. * The default ramdisk_blocksize is 1KiB and it is generally
  164. * silly to use anything else, so make sure to use 1KiB
  165. * blocksize while generating ext2fs ramdisk-images.
  166. */
  167. if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  168. rd_blocks = 0;
  169. else
  170. rd_blocks >>= 1;
  171. if (nblocks > rd_blocks) {
  172. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  173. nblocks, rd_blocks);
  174. goto done;
  175. }
  176. /*
  177. * OK, time to copy in the data
  178. */
  179. if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  180. devblocks = 0;
  181. else
  182. devblocks >>= 1;
  183. if (strcmp(from, "/initrd.image") == 0)
  184. devblocks = nblocks;
  185. if (devblocks == 0) {
  186. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  187. goto done;
  188. }
  189. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  190. if (!buf) {
  191. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  192. goto done;
  193. }
  194. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  195. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  196. for (i = 0, disk = 1; i < nblocks; i++) {
  197. if (i && (i % devblocks == 0)) {
  198. printk("done disk #%d.\n", disk++);
  199. rotate = 0;
  200. if (sys_close(in_fd)) {
  201. printk("Error closing the disk.\n");
  202. goto noclose_input;
  203. }
  204. change_floppy("disk #%d", disk);
  205. in_fd = sys_open(from, O_RDONLY, 0);
  206. if (in_fd < 0) {
  207. printk("Error opening disk.\n");
  208. goto noclose_input;
  209. }
  210. printk("Loading disk #%d... ", disk);
  211. }
  212. sys_read(in_fd, buf, BLOCK_SIZE);
  213. sys_write(out_fd, buf, BLOCK_SIZE);
  214. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  215. if (!(i % 16)) {
  216. printk("%c\b", rotator[rotate & 0x3]);
  217. rotate++;
  218. }
  219. #endif
  220. }
  221. printk("done.\n");
  222. successful_load:
  223. res = 1;
  224. done:
  225. sys_close(in_fd);
  226. noclose_input:
  227. sys_close(out_fd);
  228. out:
  229. kfree(buf);
  230. sys_unlink("/dev/ram");
  231. return res;
  232. }
  233. int __init rd_load_disk(int n)
  234. {
  235. if (rd_prompt)
  236. change_floppy("root floppy disk to be loaded into RAM disk");
  237. create_dev("/dev/root", ROOT_DEV);
  238. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  239. return rd_load_image("/dev/root");
  240. }
  241. /*
  242. * gzip declarations
  243. */
  244. #define OF(args) args
  245. #ifndef memzero
  246. #define memzero(s, n) memset ((s), 0, (n))
  247. #endif
  248. typedef unsigned char uch;
  249. typedef unsigned short ush;
  250. typedef unsigned long ulg;
  251. #define INBUFSIZ 4096
  252. #define WSIZE 0x8000 /* window size--must be a power of two, and */
  253. /* at least 32K for zip's deflate method */
  254. static uch *inbuf;
  255. static uch *window;
  256. static unsigned insize; /* valid bytes in inbuf */
  257. static unsigned inptr; /* index of next byte to be processed in inbuf */
  258. static unsigned outcnt; /* bytes in output buffer */
  259. static int exit_code;
  260. static int unzip_error;
  261. static long bytes_out;
  262. static int crd_infd, crd_outfd;
  263. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  264. /* Diagnostic functions (stubbed out) */
  265. #define Assert(cond,msg)
  266. #define Trace(x)
  267. #define Tracev(x)
  268. #define Tracevv(x)
  269. #define Tracec(c,x)
  270. #define Tracecv(c,x)
  271. #define STATIC static
  272. #define INIT __init
  273. static int __init fill_inbuf(void);
  274. static void __init flush_window(void);
  275. static void __init error(char *m);
  276. #define NO_INFLATE_MALLOC
  277. #include "../lib/inflate.c"
  278. /* ===========================================================================
  279. * Fill the input buffer. This is called only when the buffer is empty
  280. * and at least one byte is really needed.
  281. * Returning -1 does not guarantee that gunzip() will ever return.
  282. */
  283. static int __init fill_inbuf(void)
  284. {
  285. if (exit_code) return -1;
  286. insize = sys_read(crd_infd, inbuf, INBUFSIZ);
  287. if (insize == 0) {
  288. error("RAMDISK: ran out of compressed data");
  289. return -1;
  290. }
  291. inptr = 1;
  292. return inbuf[0];
  293. }
  294. /* ===========================================================================
  295. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  296. * (Used for the decompressed data only.)
  297. */
  298. static void __init flush_window(void)
  299. {
  300. ulg c = crc; /* temporary variable */
  301. unsigned n, written;
  302. uch *in, ch;
  303. written = sys_write(crd_outfd, window, outcnt);
  304. if (written != outcnt && unzip_error == 0) {
  305. printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
  306. written, outcnt, bytes_out);
  307. unzip_error = 1;
  308. }
  309. in = window;
  310. for (n = 0; n < outcnt; n++) {
  311. ch = *in++;
  312. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  313. }
  314. crc = c;
  315. bytes_out += (ulg)outcnt;
  316. outcnt = 0;
  317. }
  318. static void __init error(char *x)
  319. {
  320. printk(KERN_ERR "%s\n", x);
  321. exit_code = 1;
  322. unzip_error = 1;
  323. }
  324. static int __init crd_load(int in_fd, int out_fd)
  325. {
  326. int result;
  327. insize = 0; /* valid bytes in inbuf */
  328. inptr = 0; /* index of next byte to be processed in inbuf */
  329. outcnt = 0; /* bytes in output buffer */
  330. exit_code = 0;
  331. bytes_out = 0;
  332. crc = (ulg)0xffffffffL; /* shift register contents */
  333. crd_infd = in_fd;
  334. crd_outfd = out_fd;
  335. inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
  336. if (!inbuf) {
  337. printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
  338. return -1;
  339. }
  340. window = kmalloc(WSIZE, GFP_KERNEL);
  341. if (!window) {
  342. printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
  343. kfree(inbuf);
  344. return -1;
  345. }
  346. makecrc();
  347. result = gunzip();
  348. if (unzip_error)
  349. result = 1;
  350. kfree(inbuf);
  351. kfree(window);
  352. return result;
  353. }