do_mounts_rd.c 9.4 KB

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