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