2
0

loader-fit.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 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 "sysemu/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. const char *name;
  108. const void *data;
  109. const void *load_data;
  110. hwaddr load_addr, entry_addr;
  111. int img_off, err;
  112. size_t sz;
  113. int ret;
  114. name = fdt_getprop(itb, cfg, "kernel", NULL);
  115. if (!name) {
  116. error_setg(errp, "no kernel specified by FIT configuration");
  117. return -EINVAL;
  118. }
  119. load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
  120. if (!data) {
  121. error_prepend(errp, "unable to load kernel image from FIT: ");
  122. return -EINVAL;
  123. }
  124. err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
  125. if (err) {
  126. error_prepend(errp, "unable to read kernel load address from FIT: ");
  127. ret = err;
  128. goto out;
  129. }
  130. err = fit_image_addr(itb, img_off, "entry", &entry_addr, errp);
  131. if (err) {
  132. error_prepend(errp, "unable to read kernel entry address from FIT: ");
  133. ret = err;
  134. goto out;
  135. }
  136. if (ldr->kernel_filter) {
  137. load_data = ldr->kernel_filter(opaque, data, &load_addr, &entry_addr);
  138. }
  139. if (pend) {
  140. *pend = load_addr + sz;
  141. }
  142. load_addr = ldr->addr_to_phys(opaque, load_addr);
  143. rom_add_blob_fixed(name, load_data, sz, load_addr);
  144. ret = 0;
  145. out:
  146. g_free((void *) data);
  147. if (data != load_data) {
  148. g_free((void *) load_data);
  149. }
  150. return ret;
  151. }
  152. static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
  153. int cfg, void *opaque, const void *match_data,
  154. hwaddr kernel_end, Error **errp)
  155. {
  156. const char *name;
  157. const void *data;
  158. const void *load_data;
  159. hwaddr load_addr;
  160. int img_off, err;
  161. size_t sz;
  162. int ret;
  163. name = fdt_getprop(itb, cfg, "fdt", NULL);
  164. if (!name) {
  165. return 0;
  166. }
  167. load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
  168. if (!data) {
  169. error_prepend(errp, "unable to load FDT image from FIT: ");
  170. return -EINVAL;
  171. }
  172. err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
  173. if (err == -ENOENT) {
  174. load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
  175. error_free(*errp);
  176. } else if (err) {
  177. error_prepend(errp, "unable to read FDT load address from FIT: ");
  178. ret = err;
  179. goto out;
  180. }
  181. if (ldr->fdt_filter) {
  182. load_data = ldr->fdt_filter(opaque, data, match_data, &load_addr);
  183. }
  184. load_addr = ldr->addr_to_phys(opaque, load_addr);
  185. sz = fdt_totalsize(load_data);
  186. rom_add_blob_fixed(name, load_data, sz, load_addr);
  187. ret = 0;
  188. out:
  189. g_free((void *) data);
  190. if (data != load_data) {
  191. g_free((void *) load_data);
  192. }
  193. return ret;
  194. }
  195. static bool fit_cfg_compatible(const void *itb, int cfg, const char *compat)
  196. {
  197. const void *fdt;
  198. const char *fdt_name;
  199. bool ret;
  200. fdt_name = fdt_getprop(itb, cfg, "fdt", NULL);
  201. if (!fdt_name) {
  202. return false;
  203. }
  204. fdt = fit_load_image_alloc(itb, fdt_name, NULL, NULL, NULL);
  205. if (!fdt) {
  206. return false;
  207. }
  208. if (fdt_check_header(fdt)) {
  209. ret = false;
  210. goto out;
  211. }
  212. if (fdt_node_check_compatible(fdt, 0, compat)) {
  213. ret = false;
  214. goto out;
  215. }
  216. ret = true;
  217. out:
  218. g_free((void *) fdt);
  219. return ret;
  220. }
  221. int load_fit(const struct fit_loader *ldr, const char *filename, void *opaque)
  222. {
  223. Error *err = NULL;
  224. const struct fit_loader_match *match;
  225. const void *itb, *match_data = NULL;
  226. const char *def_cfg_name;
  227. char path[FIT_LOADER_MAX_PATH];
  228. int itb_size, configs, cfg_off, off;
  229. hwaddr kernel_end;
  230. int ret;
  231. itb = load_device_tree(filename, &itb_size);
  232. if (!itb) {
  233. return -EINVAL;
  234. }
  235. configs = fdt_path_offset(itb, "/configurations");
  236. if (configs < 0) {
  237. error_report("can't find node /configurations");
  238. ret = configs;
  239. goto out;
  240. }
  241. cfg_off = -FDT_ERR_NOTFOUND;
  242. if (ldr->matches) {
  243. for (match = ldr->matches; match->compatible; match++) {
  244. off = fdt_first_subnode(itb, configs);
  245. while (off >= 0) {
  246. if (fit_cfg_compatible(itb, off, match->compatible)) {
  247. cfg_off = off;
  248. match_data = match->data;
  249. break;
  250. }
  251. off = fdt_next_subnode(itb, off);
  252. }
  253. if (cfg_off >= 0) {
  254. break;
  255. }
  256. }
  257. }
  258. if (cfg_off < 0) {
  259. def_cfg_name = fdt_getprop(itb, configs, "default", NULL);
  260. if (def_cfg_name) {
  261. snprintf(path, sizeof(path), "/configurations/%s", def_cfg_name);
  262. cfg_off = fdt_path_offset(itb, path);
  263. }
  264. }
  265. if (cfg_off < 0) {
  266. error_report("can't find configuration");
  267. ret = cfg_off;
  268. goto out;
  269. }
  270. ret = fit_load_kernel(ldr, itb, cfg_off, opaque, &kernel_end, &err);
  271. if (ret) {
  272. error_report_err(err);
  273. goto out;
  274. }
  275. ret = fit_load_fdt(ldr, itb, cfg_off, opaque, match_data, kernel_end,
  276. &err);
  277. if (ret) {
  278. error_report_err(err);
  279. goto out;
  280. }
  281. ret = 0;
  282. out:
  283. g_free((void *) itb);
  284. return ret;
  285. }