loader-fit.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Flattened Image Tree loader.
  3. *
  4. * Copyright (c) 2016 Imagination Technologies
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #include "qemu/units.h"
  22. #include "exec/memory.h"
  23. #include "hw/loader.h"
  24. #include "hw/loader-fit.h"
  25. #include "qemu/cutils.h"
  26. #include "qemu/error-report.h"
  27. #include "system/device_tree.h"
  28. #include <libfdt.h>
  29. #include <zlib.h>
  30. #define FIT_LOADER_MAX_PATH (128)
  31. static void *fit_load_image_alloc(const void *itb, const char *name,
  32. int *poff, size_t *psz, Error **errp)
  33. {
  34. const void *data;
  35. const char *comp;
  36. void *uncomp_data;
  37. char path[FIT_LOADER_MAX_PATH];
  38. int off, sz;
  39. ssize_t uncomp_len;
  40. snprintf(path, sizeof(path), "/images/%s", name);
  41. off = fdt_path_offset(itb, path);
  42. if (off < 0) {
  43. error_setg(errp, "can't find node %s", path);
  44. return NULL;
  45. }
  46. if (poff) {
  47. *poff = off;
  48. }
  49. data = fdt_getprop(itb, off, "data", &sz);
  50. if (!data) {
  51. error_setg(errp, "can't get %s/data", path);
  52. return NULL;
  53. }
  54. comp = fdt_getprop(itb, off, "compression", NULL);
  55. if (!comp || !strcmp(comp, "none")) {
  56. if (psz) {
  57. *psz = sz;
  58. }
  59. uncomp_data = g_malloc(sz);
  60. memmove(uncomp_data, data, sz);
  61. return uncomp_data;
  62. }
  63. if (!strcmp(comp, "gzip")) {
  64. uncomp_len = UBOOT_MAX_GUNZIP_BYTES;
  65. uncomp_data = g_malloc(uncomp_len);
  66. uncomp_len = gunzip(uncomp_data, uncomp_len, (void *) data, sz);
  67. if (uncomp_len < 0) {
  68. error_setg(errp, "unable to decompress %s image", name);
  69. g_free(uncomp_data);
  70. return NULL;
  71. }
  72. uncomp_data = g_realloc(uncomp_data, uncomp_len);
  73. if (psz) {
  74. *psz = uncomp_len;
  75. }
  76. return uncomp_data;
  77. }
  78. error_setg(errp, "unknown compression '%s'", comp);
  79. return NULL;
  80. }
  81. static int fit_image_addr(const void *itb, int img, const char *name,
  82. hwaddr *addr, Error **errp)
  83. {
  84. const void *prop;
  85. int len;
  86. prop = fdt_getprop(itb, img, name, &len);
  87. if (!prop) {
  88. error_setg(errp, "can't find %s address", name);
  89. return -ENOENT;
  90. }
  91. switch (len) {
  92. case 4:
  93. *addr = fdt32_to_cpu(*(fdt32_t *)prop);
  94. return 0;
  95. case 8:
  96. *addr = fdt64_to_cpu(*(fdt64_t *)prop);
  97. return 0;
  98. default:
  99. error_setg(errp, "invalid %s address length %d", name, len);
  100. return -EINVAL;
  101. }
  102. }
  103. static int fit_load_kernel(const struct fit_loader *ldr, const void *itb,
  104. int cfg, void *opaque, hwaddr *pend,
  105. Error **errp)
  106. {
  107. ERRP_GUARD();
  108. const char *name;
  109. const void *data;
  110. const void *load_data;
  111. hwaddr load_addr, entry_addr;
  112. int img_off, err;
  113. size_t sz;
  114. int ret;
  115. name = fdt_getprop(itb, cfg, "kernel", NULL);
  116. if (!name) {
  117. error_setg(errp, "no kernel specified by FIT configuration");
  118. return -EINVAL;
  119. }
  120. load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
  121. if (!data) {
  122. error_prepend(errp, "unable to load kernel image from FIT: ");
  123. return -EINVAL;
  124. }
  125. err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
  126. if (err) {
  127. error_prepend(errp, "unable to read kernel load address from FIT: ");
  128. ret = err;
  129. goto out;
  130. }
  131. err = fit_image_addr(itb, img_off, "entry", &entry_addr, errp);
  132. if (err) {
  133. error_prepend(errp, "unable to read kernel entry address from FIT: ");
  134. ret = err;
  135. goto out;
  136. }
  137. if (ldr->kernel_filter) {
  138. load_data = ldr->kernel_filter(opaque, data, &load_addr, &entry_addr);
  139. }
  140. if (pend) {
  141. *pend = load_addr + sz;
  142. }
  143. load_addr = ldr->addr_to_phys(opaque, load_addr);
  144. rom_add_blob_fixed(name, load_data, sz, load_addr);
  145. ret = 0;
  146. out:
  147. g_free((void *) data);
  148. if (data != load_data) {
  149. g_free((void *) load_data);
  150. }
  151. return ret;
  152. }
  153. static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
  154. int cfg, void *opaque, const void *match_data,
  155. hwaddr kernel_end, void **pfdt, Error **errp)
  156. {
  157. ERRP_GUARD();
  158. Error *err = NULL;
  159. const char *name;
  160. void *data;
  161. hwaddr load_addr;
  162. int img_off;
  163. size_t sz;
  164. int ret;
  165. name = fdt_getprop(itb, cfg, "fdt", NULL);
  166. if (!name) {
  167. return 0;
  168. }
  169. data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
  170. if (!data) {
  171. error_prepend(errp, "unable to load FDT image from FIT: ");
  172. return -EINVAL;
  173. }
  174. ret = fit_image_addr(itb, img_off, "load", &load_addr, &err);
  175. if (ret == -ENOENT) {
  176. load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
  177. error_free(err);
  178. } else if (ret) {
  179. error_propagate_prepend(errp, err,
  180. "unable to read FDT load address from FIT: ");
  181. goto out;
  182. }
  183. if (ldr->fdt_filter) {
  184. void *filtered_data;
  185. filtered_data = ldr->fdt_filter(opaque, data, match_data, &load_addr);
  186. if (filtered_data != data) {
  187. g_free(data);
  188. data = filtered_data;
  189. }
  190. }
  191. load_addr = ldr->addr_to_phys(opaque, load_addr);
  192. sz = fdt_totalsize(data);
  193. rom_add_blob_fixed(name, data, sz, load_addr);
  194. *pfdt = data;
  195. return 0;
  196. out:
  197. g_free((void *) data);
  198. return ret;
  199. }
  200. static bool fit_cfg_compatible(const void *itb, int cfg, const char *compat)
  201. {
  202. const void *fdt;
  203. const char *fdt_name;
  204. bool ret;
  205. fdt_name = fdt_getprop(itb, cfg, "fdt", NULL);
  206. if (!fdt_name) {
  207. return false;
  208. }
  209. fdt = fit_load_image_alloc(itb, fdt_name, NULL, NULL, NULL);
  210. if (!fdt) {
  211. return false;
  212. }
  213. if (fdt_check_header(fdt)) {
  214. ret = false;
  215. goto out;
  216. }
  217. if (fdt_node_check_compatible(fdt, 0, compat)) {
  218. ret = false;
  219. goto out;
  220. }
  221. ret = true;
  222. out:
  223. g_free((void *) fdt);
  224. return ret;
  225. }
  226. int load_fit(const struct fit_loader *ldr, const char *filename,
  227. void **pfdt, void *opaque)
  228. {
  229. Error *err = NULL;
  230. const struct fit_loader_match *match;
  231. const void *itb, *match_data = NULL;
  232. const char *def_cfg_name;
  233. char path[FIT_LOADER_MAX_PATH];
  234. int itb_size, configs, cfg_off, off;
  235. hwaddr kernel_end = 0;
  236. int ret;
  237. itb = load_device_tree(filename, &itb_size);
  238. if (!itb) {
  239. return -EINVAL;
  240. }
  241. configs = fdt_path_offset(itb, "/configurations");
  242. if (configs < 0) {
  243. error_report("can't find node /configurations");
  244. ret = configs;
  245. goto out;
  246. }
  247. cfg_off = -FDT_ERR_NOTFOUND;
  248. if (ldr->matches) {
  249. for (match = ldr->matches; match->compatible; match++) {
  250. off = fdt_first_subnode(itb, configs);
  251. while (off >= 0) {
  252. if (fit_cfg_compatible(itb, off, match->compatible)) {
  253. cfg_off = off;
  254. match_data = match->data;
  255. break;
  256. }
  257. off = fdt_next_subnode(itb, off);
  258. }
  259. if (cfg_off >= 0) {
  260. break;
  261. }
  262. }
  263. }
  264. if (cfg_off < 0) {
  265. def_cfg_name = fdt_getprop(itb, configs, "default", NULL);
  266. if (def_cfg_name) {
  267. snprintf(path, sizeof(path), "/configurations/%s", def_cfg_name);
  268. cfg_off = fdt_path_offset(itb, path);
  269. }
  270. }
  271. if (cfg_off < 0) {
  272. error_report("can't find configuration");
  273. ret = cfg_off;
  274. goto out;
  275. }
  276. ret = fit_load_kernel(ldr, itb, cfg_off, opaque, &kernel_end, &err);
  277. if (ret) {
  278. error_report_err(err);
  279. goto out;
  280. }
  281. ret = fit_load_fdt(ldr, itb, cfg_off, opaque, match_data, kernel_end, pfdt,
  282. &err);
  283. if (ret) {
  284. error_report_err(err);
  285. goto out;
  286. }
  287. ret = 0;
  288. out:
  289. g_free((void *) itb);
  290. return ret;
  291. }