fit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * fs/partitions/fit.c
  4. * Copyright (C) 2021 Daniel Golle
  5. *
  6. * headers extracted from U-Boot mkimage sources
  7. * (C) Copyright 2008 Semihalf
  8. * (C) Copyright 2000-2005
  9. * Wolfgang Denk, DENX Software Engineering, [email protected].
  10. *
  11. * based on existing partition parsers
  12. * Copyright (C) 1991-1998 Linus Torvalds
  13. * Re-organised Feb 1998 Russell King
  14. */
  15. #define pr_fmt(fmt) fmt
  16. #include <linux/types.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/libfdt.h>
  21. #include "check.h"
  22. #define FIT_IMAGES_PATH "/images"
  23. #define FIT_CONFS_PATH "/configurations"
  24. /* hash/signature/key node */
  25. #define FIT_HASH_NODENAME "hash"
  26. #define FIT_ALGO_PROP "algo"
  27. #define FIT_VALUE_PROP "value"
  28. #define FIT_IGNORE_PROP "uboot-ignore"
  29. #define FIT_SIG_NODENAME "signature"
  30. #define FIT_KEY_REQUIRED "required"
  31. #define FIT_KEY_HINT "key-name-hint"
  32. /* cipher node */
  33. #define FIT_CIPHER_NODENAME "cipher"
  34. #define FIT_ALGO_PROP "algo"
  35. /* image node */
  36. #define FIT_DATA_PROP "data"
  37. #define FIT_DATA_POSITION_PROP "data-position"
  38. #define FIT_DATA_OFFSET_PROP "data-offset"
  39. #define FIT_DATA_SIZE_PROP "data-size"
  40. #define FIT_TIMESTAMP_PROP "timestamp"
  41. #define FIT_DESC_PROP "description"
  42. #define FIT_ARCH_PROP "arch"
  43. #define FIT_TYPE_PROP "type"
  44. #define FIT_OS_PROP "os"
  45. #define FIT_COMP_PROP "compression"
  46. #define FIT_ENTRY_PROP "entry"
  47. #define FIT_LOAD_PROP "load"
  48. /* configuration node */
  49. #define FIT_KERNEL_PROP "kernel"
  50. #define FIT_FILESYSTEM_PROP "filesystem"
  51. #define FIT_RAMDISK_PROP "ramdisk"
  52. #define FIT_FDT_PROP "fdt"
  53. #define FIT_LOADABLE_PROP "loadables"
  54. #define FIT_DEFAULT_PROP "default"
  55. #define FIT_SETUP_PROP "setup"
  56. #define FIT_FPGA_PROP "fpga"
  57. #define FIT_FIRMWARE_PROP "firmware"
  58. #define FIT_STANDALONE_PROP "standalone"
  59. #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
  60. #define MIN_FREE_SECT 16
  61. #define REMAIN_VOLNAME "rootfs_data"
  62. int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector, u64 sectors, int *slot, int add_remain)
  63. {
  64. struct address_space *mapping = state->bdev->bd_inode->i_mapping;
  65. struct page *page;
  66. void *fit, *init_fit;
  67. struct partition_meta_info *info;
  68. char tmp[sizeof(info->volname)];
  69. u64 dsize, dsectors, imgmaxsect = 0;
  70. u32 size, image_pos, image_len;
  71. const u32 *image_offset_be, *image_len_be, *image_pos_be;
  72. int ret = 1, node, images, config;
  73. const char *image_name, *image_type, *image_description, *config_default,
  74. *config_description, *config_loadables;
  75. int image_name_len, image_type_len, image_description_len, config_default_len,
  76. config_description_len, config_loadables_len;
  77. sector_t start_sect, nr_sects;
  78. size_t label_min;
  79. if (fit_start_sector % (1<<(PAGE_SHIFT - SECTOR_SHIFT)))
  80. return -ERANGE;
  81. page = read_mapping_page(mapping, fit_start_sector >> (PAGE_SHIFT - SECTOR_SHIFT), NULL);
  82. if (IS_ERR(page))
  83. return -EFAULT;
  84. if (PageError(page))
  85. return -EFAULT;
  86. init_fit = page_address(page);
  87. if (!init_fit) {
  88. put_page(page);
  89. return -EFAULT;
  90. }
  91. if (fdt_check_header(init_fit)) {
  92. put_page(page);
  93. return 0;
  94. }
  95. dsectors = get_capacity(state->bdev->bd_disk);
  96. if (sectors)
  97. dsectors = (dsectors>sectors)?sectors:dsectors;
  98. dsize = dsectors << SECTOR_SHIFT;
  99. size = fdt_totalsize(init_fit);
  100. /* silently skip non-external-data legacy FIT images */
  101. if (size > PAGE_SIZE) {
  102. put_page(page);
  103. return 0;
  104. }
  105. if (size >= dsize) {
  106. state->access_beyond_eod = 1;
  107. put_page(page);
  108. return -EFBIG;
  109. }
  110. fit = kmemdup(init_fit, size, GFP_KERNEL);
  111. put_page(page);
  112. if (!fit)
  113. return -ENOMEM;
  114. config = fdt_path_offset(fit, FIT_CONFS_PATH);
  115. if (config < 0) {
  116. printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_CONFS_PATH, images);
  117. ret = -ENOENT;
  118. goto ret_out;
  119. }
  120. config_default = fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
  121. if (!config_default) {
  122. printk(KERN_ERR "FIT: Cannot find default configuration\n");
  123. ret = -ENOENT;
  124. goto ret_out;
  125. }
  126. node = fdt_subnode_offset(fit, config, config_default);
  127. if (node < 0) {
  128. printk(KERN_ERR "FIT: Cannot find %s node: %d\n", config_default, node);
  129. ret = -ENOENT;
  130. goto ret_out;
  131. }
  132. config_description = fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
  133. config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP, &config_loadables_len);
  134. printk(KERN_DEBUG "FIT: Default configuration: \"%s\"%s%s%s\n", config_default,
  135. config_description?" (":"", config_description?:"", config_description?")":"");
  136. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  137. if (images < 0) {
  138. printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
  139. ret = -EINVAL;
  140. goto ret_out;
  141. }
  142. fdt_for_each_subnode(node, fit, images) {
  143. image_name = fdt_get_name(fit, node, &image_name_len);
  144. image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
  145. image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
  146. image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
  147. image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
  148. if (!image_name || !image_type || !image_len_be)
  149. continue;
  150. image_len = be32_to_cpu(*image_len_be);
  151. if (!image_len)
  152. continue;
  153. if (image_offset_be)
  154. image_pos = be32_to_cpu(*image_offset_be) + size;
  155. else if (image_pos_be)
  156. image_pos = be32_to_cpu(*image_pos_be);
  157. else
  158. continue;
  159. image_description = fdt_getprop(fit, node, FIT_DESC_PROP, &image_description_len);
  160. printk(KERN_DEBUG "FIT: %16s sub-image 0x%08x..0x%08x \"%s\" %s%s%s\n",
  161. image_type, image_pos, image_pos + image_len - 1, image_name,
  162. image_description?"(":"", image_description?:"", image_description?") ":"");
  163. if (strcmp(image_type, FIT_FILESYSTEM_PROP))
  164. continue;
  165. if (image_pos & ((1 << PAGE_SHIFT)-1)) {
  166. printk(KERN_ERR "FIT: image %s start not aligned to page boundaries, skipping\n", image_name);
  167. continue;
  168. }
  169. if (image_len & ((1 << PAGE_SHIFT)-1)) {
  170. printk(KERN_ERR "FIT: sub-image %s end not aligned to page boundaries, skipping\n", image_name);
  171. continue;
  172. }
  173. start_sect = image_pos >> SECTOR_SHIFT;
  174. nr_sects = image_len >> SECTOR_SHIFT;
  175. imgmaxsect = (imgmaxsect < (start_sect + nr_sects))?(start_sect + nr_sects):imgmaxsect;
  176. if (start_sect + nr_sects > dsectors) {
  177. state->access_beyond_eod = 1;
  178. continue;
  179. }
  180. put_partition(state, ++(*slot), fit_start_sector + start_sect, nr_sects);
  181. state->parts[*slot].flags = 0;
  182. info = &state->parts[*slot].info;
  183. label_min = min_t(int, sizeof(info->volname) - 1, image_name_len);
  184. strncpy(info->volname, image_name, label_min);
  185. info->volname[label_min] = '\0';
  186. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  187. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  188. state->parts[*slot].has_info = true;
  189. if (config_loadables && !strcmp(image_name, config_loadables)) {
  190. printk(KERN_DEBUG "FIT: selecting configured loadable \"%s\" to be root filesystem\n", image_name);
  191. state->parts[*slot].flags |= ADDPART_FLAG_ROOTDEV;
  192. }
  193. }
  194. if (add_remain && (imgmaxsect + MIN_FREE_SECT) < dsectors) {
  195. put_partition(state, ++(*slot), fit_start_sector + imgmaxsect, dsectors - imgmaxsect);
  196. state->parts[*slot].flags = 0;
  197. info = &state->parts[*slot].info;
  198. strcpy(info->volname, REMAIN_VOLNAME);
  199. snprintf(tmp, sizeof(tmp), "(%s)", REMAIN_VOLNAME);
  200. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  201. }
  202. ret_out:
  203. kfree(fit);
  204. return ret;
  205. }
  206. int fit_partition(struct parsed_partitions *state) {
  207. int slot = 0;
  208. return parse_fit_partitions(state, 0, 0, &slot, 0);
  209. }