do_mounts_rd.c 8.4 KB

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