loader-fit.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 const 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. data = g_realloc(uncomp_data, uncomp_len);
  73. if (psz) {
  74. *psz = uncomp_len;
  75. }
  76. return 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, Error **errp)
  156. {
  157. ERRP_GUARD();
  158. Error *err = NULL;
  159. const char *name;
  160. const void *data;
  161. const void *load_data;
  162. hwaddr load_addr;
  163. int img_off;
  164. size_t sz;
  165. int ret;
  166. name = fdt_getprop(itb, cfg, "fdt", NULL);
  167. if (!name) {
  168. return 0;
  169. }
  170. load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
  171. if (!data) {
  172. error_prepend(errp, "unable to load FDT image from FIT: ");
  173. return -EINVAL;
  174. }
  175. ret = fit_image_addr(itb, img_off, "load", &load_addr, &err);
  176. if (ret == -ENOENT) {
  177. load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
  178. error_free(err);
  179. } else if (ret) {
  180. error_propagate_prepend(errp, err,
  181. "unable to read FDT load address from FIT: ");
  182. goto out;
  183. }
  184. if (ldr->fdt_filter) {
  185. load_data = ldr->fdt_filter(opaque, data, match_data, &load_addr);
  186. }
  187. load_addr = ldr->addr_to_phys(opaque, load_addr);
  188. sz = fdt_totalsize(load_data);
  189. rom_add_blob_fixed(name, load_data, sz, load_addr);
  190. ret = 0;
  191. out:
  192. g_free((void *) data);
  193. if (data != load_data) {
  194. g_free((void *) load_data);
  195. }
  196. return ret;
  197. }
  198. static bool fit_cfg_compatible(const void *itb, int cfg, const char *compat)
  199. {
  200. const void *fdt;
  201. const char *fdt_name;
  202. bool ret;
  203. fdt_name = fdt_getprop(itb, cfg, "fdt", NULL);
  204. if (!fdt_name) {
  205. return false;
  206. }
  207. fdt = fit_load_image_alloc(itb, fdt_name, NULL, NULL, NULL);
  208. if (!fdt) {
  209. return false;
  210. }
  211. if (fdt_check_header(fdt)) {
  212. ret = false;
  213. goto out;
  214. }
  215. if (fdt_node_check_compatible(fdt, 0, compat)) {
  216. ret = false;
  217. goto out;
  218. }
  219. ret = true;
  220. out:
  221. g_free((void *) fdt);
  222. return ret;
  223. }
  224. int load_fit(const struct fit_loader *ldr, const char *filename, void *opaque)
  225. {
  226. Error *err = NULL;
  227. const struct fit_loader_match *match;
  228. const void *itb, *match_data = NULL;
  229. const char *def_cfg_name;
  230. char path[FIT_LOADER_MAX_PATH];
  231. int itb_size, configs, cfg_off, off;
  232. hwaddr kernel_end = 0;
  233. int ret;
  234. itb = load_device_tree(filename, &itb_size);
  235. if (!itb) {
  236. return -EINVAL;
  237. }
  238. configs = fdt_path_offset(itb, "/configurations");
  239. if (configs < 0) {
  240. error_report("can't find node /configurations");
  241. ret = configs;
  242. goto out;
  243. }
  244. cfg_off = -FDT_ERR_NOTFOUND;
  245. if (ldr->matches) {
  246. for (match = ldr->matches; match->compatible; match++) {
  247. off = fdt_first_subnode(itb, configs);
  248. while (off >= 0) {
  249. if (fit_cfg_compatible(itb, off, match->compatible)) {
  250. cfg_off = off;
  251. match_data = match->data;
  252. break;
  253. }
  254. off = fdt_next_subnode(itb, off);
  255. }
  256. if (cfg_off >= 0) {
  257. break;
  258. }
  259. }
  260. }
  261. if (cfg_off < 0) {
  262. def_cfg_name = fdt_getprop(itb, configs, "default", NULL);
  263. if (def_cfg_name) {
  264. snprintf(path, sizeof(path), "/configurations/%s", def_cfg_name);
  265. cfg_off = fdt_path_offset(itb, path);
  266. }
  267. }
  268. if (cfg_off < 0) {
  269. error_report("can't find configuration");
  270. ret = cfg_off;
  271. goto out;
  272. }
  273. ret = fit_load_kernel(ldr, itb, cfg_off, opaque, &kernel_end, &err);
  274. if (ret) {
  275. error_report_err(err);
  276. goto out;
  277. }
  278. ret = fit_load_fdt(ldr, itb, cfg_off, opaque, match_data, kernel_end,
  279. &err);
  280. if (ret) {
  281. error_report_err(err);
  282. goto out;
  283. }
  284. ret = 0;
  285. out:
  286. g_free((void *) itb);
  287. return ret;
  288. }