do_mounts_rd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <linux/slab.h>
  10. #include "do_mounts.h"
  11. #include "../fs/squashfs/squashfs_fs.h"
  12. #include <linux/decompress/generic.h>
  13. int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
  14. static int __init prompt_ramdisk(char *str)
  15. {
  16. rd_prompt = simple_strtol(str,NULL,0) & 1;
  17. return 1;
  18. }
  19. __setup("prompt_ramdisk=", prompt_ramdisk);
  20. int __initdata rd_image_start; /* starting block # of image */
  21. static int __init ramdisk_start_setup(char *str)
  22. {
  23. rd_image_start = simple_strtol(str,NULL,0);
  24. return 1;
  25. }
  26. __setup("ramdisk_start=", ramdisk_start_setup);
  27. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
  28. /*
  29. * This routine tries to find a RAM disk image to load, and returns the
  30. * number of blocks to read for a non-compressed image, 0 if the image
  31. * is a compressed image, and -1 if an image with the right magic
  32. * numbers could not be found.
  33. *
  34. * We currently check for the following magic numbers:
  35. * minix
  36. * ext2
  37. * romfs
  38. * cramfs
  39. * squashfs
  40. * gzip
  41. */
  42. static int __init
  43. identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
  44. {
  45. const int size = 512;
  46. struct minix_super_block *minixsb;
  47. struct romfs_super_block *romfsb;
  48. struct cramfs_super *cramfsb;
  49. struct squashfs_super_block *squashfsb;
  50. int nblocks = -1;
  51. unsigned char *buf;
  52. const char *compress_name;
  53. unsigned long n;
  54. buf = kmalloc(size, GFP_KERNEL);
  55. if (!buf)
  56. return -ENOMEM;
  57. minixsb = (struct minix_super_block *) buf;
  58. romfsb = (struct romfs_super_block *) buf;
  59. cramfsb = (struct cramfs_super *) buf;
  60. squashfsb = (struct squashfs_super_block *) buf;
  61. memset(buf, 0xe5, size);
  62. /*
  63. * Read block 0 to test for compressed kernel
  64. */
  65. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  66. sys_read(fd, buf, size);
  67. *decompressor = decompress_method(buf, size, &compress_name);
  68. if (compress_name) {
  69. printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
  70. compress_name, start_block);
  71. if (!*decompressor)
  72. printk(KERN_EMERG
  73. "RAMDISK: %s decompressor not configured!\n",
  74. compress_name);
  75. nblocks = 0;
  76. goto done;
  77. }
  78. /* romfs is at block zero too */
  79. if (romfsb->word0 == ROMSB_WORD0 &&
  80. romfsb->word1 == ROMSB_WORD1) {
  81. printk(KERN_NOTICE
  82. "RAMDISK: romfs filesystem found at block %d\n",
  83. start_block);
  84. nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  85. goto done;
  86. }
  87. if (cramfsb->magic == CRAMFS_MAGIC) {
  88. printk(KERN_NOTICE
  89. "RAMDISK: cramfs filesystem found at block %d\n",
  90. start_block);
  91. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  92. goto done;
  93. }
  94. /* squashfs is at block zero too */
  95. if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
  96. printk(KERN_NOTICE
  97. "RAMDISK: squashfs filesystem found at block %d\n",
  98. start_block);
  99. nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
  100. >> BLOCK_SIZE_BITS;
  101. goto done;
  102. }
  103. /*
  104. * Read 512 bytes further to check if cramfs is padded
  105. */
  106. sys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
  107. sys_read(fd, buf, size);
  108. if (cramfsb->magic == CRAMFS_MAGIC) {
  109. printk(KERN_NOTICE
  110. "RAMDISK: cramfs filesystem found at block %d\n",
  111. start_block);
  112. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  113. goto done;
  114. }
  115. /*
  116. * Read block 1 to test for minix and ext2 superblock
  117. */
  118. sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
  119. sys_read(fd, buf, size);
  120. /* Try minix */
  121. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  122. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  123. printk(KERN_NOTICE
  124. "RAMDISK: Minix filesystem found at block %d\n",
  125. start_block);
  126. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  127. goto done;
  128. }
  129. /* Try ext2 */
  130. n = ext2_image_size(buf);
  131. if (n) {
  132. printk(KERN_NOTICE
  133. "RAMDISK: ext2 filesystem found at block %d\n",
  134. start_block);
  135. nblocks = n;
  136. goto done;
  137. }
  138. printk(KERN_NOTICE
  139. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  140. start_block);
  141. done:
  142. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  143. kfree(buf);
  144. return nblocks;
  145. }
  146. int __init rd_load_image(char *from)
  147. {
  148. int res = 0;
  149. int in_fd, out_fd;
  150. unsigned long rd_blocks, devblocks;
  151. int nblocks, i, disk;
  152. char *buf = NULL;
  153. unsigned short rotate = 0;
  154. decompress_fn decompressor = NULL;
  155. #if !defined(CONFIG_S390)
  156. char rotator[4] = { '|' , '/' , '-' , '\\' };
  157. #endif
  158. out_fd = sys_open((const char __user __force *) "/dev/ram", O_RDWR, 0);
  159. if (out_fd < 0)
  160. goto out;
  161. in_fd = sys_open(from, O_RDONLY, 0);
  162. if (in_fd < 0)
  163. goto noclose_input;
  164. nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
  165. if (nblocks < 0)
  166. goto done;
  167. if (nblocks == 0) {
  168. if (crd_load(in_fd, out_fd, decompressor) == 0)
  169. goto successful_load;
  170. goto done;
  171. }
  172. /*
  173. * NOTE NOTE: nblocks is not actually blocks but
  174. * the number of kibibytes of data to load into a ramdisk.
  175. * So any ramdisk block size that is a multiple of 1KiB should
  176. * work when the appropriate ramdisk_blocksize is specified
  177. * on the command line.
  178. *
  179. * The default ramdisk_blocksize is 1KiB and it is generally
  180. * silly to use anything else, so make sure to use 1KiB
  181. * blocksize while generating ext2fs ramdisk-images.
  182. */
  183. if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  184. rd_blocks = 0;
  185. else
  186. rd_blocks >>= 1;
  187. if (nblocks > rd_blocks) {
  188. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  189. nblocks, rd_blocks);
  190. goto done;
  191. }
  192. /*
  193. * OK, time to copy in the data
  194. */
  195. if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  196. devblocks = 0;
  197. else
  198. devblocks >>= 1;
  199. if (strcmp(from, "/initrd.image") == 0)
  200. devblocks = nblocks;
  201. if (devblocks == 0) {
  202. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  203. goto done;
  204. }
  205. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  206. if (!buf) {
  207. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  208. goto done;
  209. }
  210. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  211. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  212. for (i = 0, disk = 1; i < nblocks; i++) {
  213. if (i && (i % devblocks == 0)) {
  214. printk("done disk #%d.\n", disk++);
  215. rotate = 0;
  216. if (sys_close(in_fd)) {
  217. printk("Error closing the disk.\n");
  218. goto noclose_input;
  219. }
  220. change_floppy("disk #%d", disk);
  221. in_fd = sys_open(from, O_RDONLY, 0);
  222. if (in_fd < 0) {
  223. printk("Error opening disk.\n");
  224. goto noclose_input;
  225. }
  226. printk("Loading disk #%d... ", disk);
  227. }
  228. sys_read(in_fd, buf, BLOCK_SIZE);
  229. sys_write(out_fd, buf, BLOCK_SIZE);
  230. #if !defined(CONFIG_S390)
  231. if (!(i % 16)) {
  232. printk("%c\b", rotator[rotate & 0x3]);
  233. rotate++;
  234. }
  235. #endif
  236. }
  237. printk("done.\n");
  238. successful_load:
  239. res = 1;
  240. done:
  241. sys_close(in_fd);
  242. noclose_input:
  243. sys_close(out_fd);
  244. out:
  245. kfree(buf);
  246. sys_unlink((const char __user __force *) "/dev/ram");
  247. return res;
  248. }
  249. int __init rd_load_disk(int n)
  250. {
  251. if (rd_prompt)
  252. change_floppy("root floppy disk to be loaded into RAM disk");
  253. create_dev("/dev/root", ROOT_DEV);
  254. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  255. return rd_load_image("/dev/root");
  256. }
  257. static int exit_code;
  258. static int decompress_error;
  259. static int crd_infd, crd_outfd;
  260. static int __init compr_fill(void *buf, unsigned int len)
  261. {
  262. int r = sys_read(crd_infd, buf, len);
  263. if (r < 0)
  264. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  265. else if (r == 0)
  266. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  267. return r;
  268. }
  269. static int __init compr_flush(void *window, unsigned int outcnt)
  270. {
  271. int written = sys_write(crd_outfd, window, outcnt);
  272. if (written != outcnt) {
  273. if (decompress_error == 0)
  274. printk(KERN_ERR
  275. "RAMDISK: incomplete write (%d != %d)\n",
  276. written, outcnt);
  277. decompress_error = 1;
  278. return -1;
  279. }
  280. return outcnt;
  281. }
  282. static void __init error(char *x)
  283. {
  284. printk(KERN_ERR "%s\n", x);
  285. exit_code = 1;
  286. decompress_error = 1;
  287. }
  288. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
  289. {
  290. int result;
  291. crd_infd = in_fd;
  292. crd_outfd = out_fd;
  293. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  294. if (decompress_error)
  295. result = 1;
  296. return result;
  297. }