do_mounts_rd.c 8.7 KB

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