do_mounts_rd.c 7.4 KB

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