do_mounts_rd.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. */
  190. if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  191. rd_blocks = 0;
  192. else
  193. rd_blocks >>= 1;
  194. if (nblocks > rd_blocks) {
  195. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  196. nblocks, rd_blocks);
  197. goto done;
  198. }
  199. /*
  200. * OK, time to copy in the data
  201. */
  202. if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  203. devblocks = 0;
  204. else
  205. devblocks >>= 1;
  206. if (strcmp(from, "/initrd.image") == 0)
  207. devblocks = nblocks;
  208. if (devblocks == 0) {
  209. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  210. goto done;
  211. }
  212. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  213. if (!buf) {
  214. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  215. goto done;
  216. }
  217. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  218. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  219. for (i = 0, disk = 1; i < nblocks; i++) {
  220. if (i && (i % devblocks == 0)) {
  221. printk("done disk #%d.\n", disk++);
  222. rotate = 0;
  223. if (sys_close(in_fd)) {
  224. printk("Error closing the disk.\n");
  225. goto noclose_input;
  226. }
  227. change_floppy("disk #%d", disk);
  228. in_fd = sys_open(from, O_RDONLY, 0);
  229. if (in_fd < 0) {
  230. printk("Error opening disk.\n");
  231. goto noclose_input;
  232. }
  233. printk("Loading disk #%d... ", disk);
  234. }
  235. sys_read(in_fd, buf, BLOCK_SIZE);
  236. sys_write(out_fd, buf, BLOCK_SIZE);
  237. #if !defined(CONFIG_S390)
  238. if (!(i % 16)) {
  239. pr_cont("%c\b", rotator[rotate & 0x3]);
  240. rotate++;
  241. }
  242. #endif
  243. }
  244. printk("done.\n");
  245. successful_load:
  246. res = 1;
  247. done:
  248. sys_close(in_fd);
  249. noclose_input:
  250. sys_close(out_fd);
  251. out:
  252. kfree(buf);
  253. sys_unlink("/dev/ram");
  254. return res;
  255. }
  256. int __init rd_load_disk(int n)
  257. {
  258. if (rd_prompt)
  259. change_floppy("root floppy disk to be loaded into RAM disk");
  260. create_dev("/dev/root", ROOT_DEV);
  261. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  262. return rd_load_image("/dev/root");
  263. }
  264. static int exit_code;
  265. static int decompress_error;
  266. static int crd_infd, crd_outfd;
  267. static long __init compr_fill(void *buf, unsigned long len)
  268. {
  269. long r = sys_read(crd_infd, buf, len);
  270. if (r < 0)
  271. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  272. else if (r == 0)
  273. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  274. return r;
  275. }
  276. static long __init compr_flush(void *window, unsigned long outcnt)
  277. {
  278. long written = sys_write(crd_outfd, window, outcnt);
  279. if (written != outcnt) {
  280. if (decompress_error == 0)
  281. printk(KERN_ERR
  282. "RAMDISK: incomplete write (%ld != %ld)\n",
  283. written, outcnt);
  284. decompress_error = 1;
  285. return -1;
  286. }
  287. return outcnt;
  288. }
  289. static void __init error(char *x)
  290. {
  291. printk(KERN_ERR "%s\n", x);
  292. exit_code = 1;
  293. decompress_error = 1;
  294. }
  295. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
  296. {
  297. int result;
  298. crd_infd = in_fd;
  299. crd_outfd = out_fd;
  300. if (!deco) {
  301. pr_emerg("Invalid ramdisk decompression routine. "
  302. "Select appropriate config option.\n");
  303. panic("Could not decompress initial ramdisk image.");
  304. }
  305. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  306. if (decompress_error)
  307. result = 1;
  308. return result;
  309. }