do_mounts_rd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 block 1 to test for minix and ext2 superblock
  106. */
  107. sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
  108. sys_read(fd, buf, size);
  109. /* Try minix */
  110. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  111. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  112. printk(KERN_NOTICE
  113. "RAMDISK: Minix filesystem found at block %d\n",
  114. start_block);
  115. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  116. goto done;
  117. }
  118. /* Try ext2 */
  119. if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
  120. printk(KERN_NOTICE
  121. "RAMDISK: ext2 filesystem found at block %d\n",
  122. start_block);
  123. nblocks = le32_to_cpu(ext2sb->s_blocks_count) <<
  124. le32_to_cpu(ext2sb->s_log_block_size);
  125. goto done;
  126. }
  127. printk(KERN_NOTICE
  128. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  129. start_block);
  130. done:
  131. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  132. kfree(buf);
  133. return nblocks;
  134. }
  135. int __init rd_load_image(char *from)
  136. {
  137. int res = 0;
  138. int in_fd, out_fd;
  139. unsigned long rd_blocks, devblocks;
  140. int nblocks, i, disk;
  141. char *buf = NULL;
  142. unsigned short rotate = 0;
  143. decompress_fn decompressor = NULL;
  144. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  145. char rotator[4] = { '|' , '/' , '-' , '\\' };
  146. #endif
  147. out_fd = sys_open((const char __user __force *) "/dev/ram", O_RDWR, 0);
  148. if (out_fd < 0)
  149. goto out;
  150. in_fd = sys_open(from, O_RDONLY, 0);
  151. if (in_fd < 0)
  152. goto noclose_input;
  153. nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
  154. if (nblocks < 0)
  155. goto done;
  156. if (nblocks == 0) {
  157. if (crd_load(in_fd, out_fd, decompressor) == 0)
  158. goto successful_load;
  159. goto done;
  160. }
  161. /*
  162. * NOTE NOTE: nblocks is not actually blocks but
  163. * the number of kibibytes of data to load into a ramdisk.
  164. * So any ramdisk block size that is a multiple of 1KiB should
  165. * work when the appropriate ramdisk_blocksize is specified
  166. * on the command line.
  167. *
  168. * The default ramdisk_blocksize is 1KiB and it is generally
  169. * silly to use anything else, so make sure to use 1KiB
  170. * blocksize while generating ext2fs ramdisk-images.
  171. */
  172. if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  173. rd_blocks = 0;
  174. else
  175. rd_blocks >>= 1;
  176. if (nblocks > rd_blocks) {
  177. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  178. nblocks, rd_blocks);
  179. goto done;
  180. }
  181. /*
  182. * OK, time to copy in the data
  183. */
  184. if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  185. devblocks = 0;
  186. else
  187. devblocks >>= 1;
  188. if (strcmp(from, "/initrd.image") == 0)
  189. devblocks = nblocks;
  190. if (devblocks == 0) {
  191. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  192. goto done;
  193. }
  194. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  195. if (!buf) {
  196. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  197. goto done;
  198. }
  199. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  200. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  201. for (i = 0, disk = 1; i < nblocks; i++) {
  202. if (i && (i % devblocks == 0)) {
  203. printk("done disk #%d.\n", disk++);
  204. rotate = 0;
  205. if (sys_close(in_fd)) {
  206. printk("Error closing the disk.\n");
  207. goto noclose_input;
  208. }
  209. change_floppy("disk #%d", disk);
  210. in_fd = sys_open(from, O_RDONLY, 0);
  211. if (in_fd < 0) {
  212. printk("Error opening disk.\n");
  213. goto noclose_input;
  214. }
  215. printk("Loading disk #%d... ", disk);
  216. }
  217. sys_read(in_fd, buf, BLOCK_SIZE);
  218. sys_write(out_fd, buf, BLOCK_SIZE);
  219. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  220. if (!(i % 16)) {
  221. printk("%c\b", rotator[rotate & 0x3]);
  222. rotate++;
  223. }
  224. #endif
  225. }
  226. printk("done.\n");
  227. successful_load:
  228. res = 1;
  229. done:
  230. sys_close(in_fd);
  231. noclose_input:
  232. sys_close(out_fd);
  233. out:
  234. kfree(buf);
  235. sys_unlink((const char __user __force *) "/dev/ram");
  236. return res;
  237. }
  238. int __init rd_load_disk(int n)
  239. {
  240. if (rd_prompt)
  241. change_floppy("root floppy disk to be loaded into RAM disk");
  242. create_dev("/dev/root", ROOT_DEV);
  243. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  244. return rd_load_image("/dev/root");
  245. }
  246. static int exit_code;
  247. static int decompress_error;
  248. static int crd_infd, crd_outfd;
  249. static int __init compr_fill(void *buf, unsigned int len)
  250. {
  251. int r = sys_read(crd_infd, buf, len);
  252. if (r < 0)
  253. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  254. else if (r == 0)
  255. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  256. return r;
  257. }
  258. static int __init compr_flush(void *window, unsigned int outcnt)
  259. {
  260. int written = sys_write(crd_outfd, window, outcnt);
  261. if (written != outcnt) {
  262. if (decompress_error == 0)
  263. printk(KERN_ERR
  264. "RAMDISK: incomplete write (%d != %d)\n",
  265. written, outcnt);
  266. decompress_error = 1;
  267. return -1;
  268. }
  269. return outcnt;
  270. }
  271. static void __init error(char *x)
  272. {
  273. printk(KERN_ERR "%s\n", x);
  274. exit_code = 1;
  275. decompress_error = 1;
  276. }
  277. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
  278. {
  279. int result;
  280. crd_infd = in_fd;
  281. crd_outfd = out_fd;
  282. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  283. if (decompress_error)
  284. result = 1;
  285. return result;
  286. }