do_mounts_rd.c 8.0 KB

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