do_mounts_rd.c 8.4 KB

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