do_mounts_rd.c 8.6 KB

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