151b58561SPaul Burton /*
251b58561SPaul Burton * Flattened Image Tree loader.
351b58561SPaul Burton *
451b58561SPaul Burton * Copyright (c) 2016 Imagination Technologies
551b58561SPaul Burton *
651b58561SPaul Burton * This library is free software; you can redistribute it and/or
751b58561SPaul Burton * modify it under the terms of the GNU Lesser General Public
851b58561SPaul Burton * License as published by the Free Software Foundation; either
94a129ccdSChetan Pant * version 2.1 of the License, or (at your option) any later version.
1051b58561SPaul Burton *
1151b58561SPaul Burton * This library is distributed in the hope that it will be useful,
1251b58561SPaul Burton * but WITHOUT ANY WARRANTY; without even the implied warranty of
1351b58561SPaul Burton * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1451b58561SPaul Burton * Lesser General Public License for more details.
1551b58561SPaul Burton *
1651b58561SPaul Burton * You should have received a copy of the GNU Lesser General Public
1751b58561SPaul Burton * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1851b58561SPaul Burton */
1951b58561SPaul Burton
2051b58561SPaul Burton #include "qemu/osdep.h"
213eb99edbSMarkus Armbruster #include "qapi/error.h"
22d23b6caaSPhilippe Mathieu-Daudé #include "qemu/units.h"
2351b58561SPaul Burton #include "exec/memory.h"
2451b58561SPaul Burton #include "hw/loader.h"
2551b58561SPaul Burton #include "hw/loader-fit.h"
2651b58561SPaul Burton #include "qemu/cutils.h"
2751b58561SPaul Burton #include "qemu/error-report.h"
2851b58561SPaul Burton #include "sysemu/device_tree.h"
2951b58561SPaul Burton
3051b58561SPaul Burton #include <libfdt.h>
3151b58561SPaul Burton #include <zlib.h>
3251b58561SPaul Burton
3351b58561SPaul Burton #define FIT_LOADER_MAX_PATH (128)
3451b58561SPaul Burton
fit_load_image_alloc(const void * itb,const char * name,int * poff,size_t * psz,Error ** errp)3551b58561SPaul Burton static const void *fit_load_image_alloc(const void *itb, const char *name,
363eb99edbSMarkus Armbruster int *poff, size_t *psz, Error **errp)
3751b58561SPaul Burton {
3851b58561SPaul Burton const void *data;
3951b58561SPaul Burton const char *comp;
4051b58561SPaul Burton void *uncomp_data;
4151b58561SPaul Burton char path[FIT_LOADER_MAX_PATH];
4251b58561SPaul Burton int off, sz;
4351b58561SPaul Burton ssize_t uncomp_len;
4451b58561SPaul Burton
4551b58561SPaul Burton snprintf(path, sizeof(path), "/images/%s", name);
4651b58561SPaul Burton
4751b58561SPaul Burton off = fdt_path_offset(itb, path);
4851b58561SPaul Burton if (off < 0) {
493eb99edbSMarkus Armbruster error_setg(errp, "can't find node %s", path);
5051b58561SPaul Burton return NULL;
5151b58561SPaul Burton }
5251b58561SPaul Burton if (poff) {
5351b58561SPaul Burton *poff = off;
5451b58561SPaul Burton }
5551b58561SPaul Burton
5651b58561SPaul Burton data = fdt_getprop(itb, off, "data", &sz);
5751b58561SPaul Burton if (!data) {
583eb99edbSMarkus Armbruster error_setg(errp, "can't get %s/data", path);
5951b58561SPaul Burton return NULL;
6051b58561SPaul Burton }
6151b58561SPaul Burton
6251b58561SPaul Burton comp = fdt_getprop(itb, off, "compression", NULL);
6351b58561SPaul Burton if (!comp || !strcmp(comp, "none")) {
6451b58561SPaul Burton if (psz) {
6551b58561SPaul Burton *psz = sz;
6651b58561SPaul Burton }
6751b58561SPaul Burton uncomp_data = g_malloc(sz);
6851b58561SPaul Burton memmove(uncomp_data, data, sz);
6951b58561SPaul Burton return uncomp_data;
7051b58561SPaul Burton }
7151b58561SPaul Burton
7251b58561SPaul Burton if (!strcmp(comp, "gzip")) {
7351b58561SPaul Burton uncomp_len = UBOOT_MAX_GUNZIP_BYTES;
7451b58561SPaul Burton uncomp_data = g_malloc(uncomp_len);
7551b58561SPaul Burton
7651b58561SPaul Burton uncomp_len = gunzip(uncomp_data, uncomp_len, (void *) data, sz);
7751b58561SPaul Burton if (uncomp_len < 0) {
783eb99edbSMarkus Armbruster error_setg(errp, "unable to decompress %s image", name);
7951b58561SPaul Burton g_free(uncomp_data);
8051b58561SPaul Burton return NULL;
8151b58561SPaul Burton }
8251b58561SPaul Burton
8351b58561SPaul Burton data = g_realloc(uncomp_data, uncomp_len);
8451b58561SPaul Burton if (psz) {
8551b58561SPaul Burton *psz = uncomp_len;
8651b58561SPaul Burton }
8751b58561SPaul Burton return data;
8851b58561SPaul Burton }
8951b58561SPaul Burton
903eb99edbSMarkus Armbruster error_setg(errp, "unknown compression '%s'", comp);
9151b58561SPaul Burton return NULL;
9251b58561SPaul Burton }
9351b58561SPaul Burton
fit_image_addr(const void * itb,int img,const char * name,hwaddr * addr,Error ** errp)9451b58561SPaul Burton static int fit_image_addr(const void *itb, int img, const char *name,
953eb99edbSMarkus Armbruster hwaddr *addr, Error **errp)
9651b58561SPaul Burton {
9751b58561SPaul Burton const void *prop;
9851b58561SPaul Burton int len;
9951b58561SPaul Burton
10051b58561SPaul Burton prop = fdt_getprop(itb, img, name, &len);
10151b58561SPaul Burton if (!prop) {
1023eb99edbSMarkus Armbruster error_setg(errp, "can't find %s address", name);
10351b58561SPaul Burton return -ENOENT;
10451b58561SPaul Burton }
10551b58561SPaul Burton
10651b58561SPaul Burton switch (len) {
10751b58561SPaul Burton case 4:
10851b58561SPaul Burton *addr = fdt32_to_cpu(*(fdt32_t *)prop);
10951b58561SPaul Burton return 0;
11051b58561SPaul Burton case 8:
11151b58561SPaul Burton *addr = fdt64_to_cpu(*(fdt64_t *)prop);
11251b58561SPaul Burton return 0;
11351b58561SPaul Burton default:
1143eb99edbSMarkus Armbruster error_setg(errp, "invalid %s address length %d", name, len);
11551b58561SPaul Burton return -EINVAL;
11651b58561SPaul Burton }
11751b58561SPaul Burton }
11851b58561SPaul Burton
fit_load_kernel(const struct fit_loader * ldr,const void * itb,int cfg,void * opaque,hwaddr * pend,Error ** errp)11951b58561SPaul Burton static int fit_load_kernel(const struct fit_loader *ldr, const void *itb,
1203eb99edbSMarkus Armbruster int cfg, void *opaque, hwaddr *pend,
1213eb99edbSMarkus Armbruster Error **errp)
12251b58561SPaul Burton {
123bfd65b4bSZhao Liu ERRP_GUARD();
12451b58561SPaul Burton const char *name;
12551b58561SPaul Burton const void *data;
12651b58561SPaul Burton const void *load_data;
12751b58561SPaul Burton hwaddr load_addr, entry_addr;
12851b58561SPaul Burton int img_off, err;
12951b58561SPaul Burton size_t sz;
13051b58561SPaul Burton int ret;
13151b58561SPaul Burton
13251b58561SPaul Burton name = fdt_getprop(itb, cfg, "kernel", NULL);
13351b58561SPaul Burton if (!name) {
1343eb99edbSMarkus Armbruster error_setg(errp, "no kernel specified by FIT configuration");
13551b58561SPaul Burton return -EINVAL;
13651b58561SPaul Burton }
13751b58561SPaul Burton
1383eb99edbSMarkus Armbruster load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
13951b58561SPaul Burton if (!data) {
1403eb99edbSMarkus Armbruster error_prepend(errp, "unable to load kernel image from FIT: ");
14151b58561SPaul Burton return -EINVAL;
14251b58561SPaul Burton }
14351b58561SPaul Burton
1443eb99edbSMarkus Armbruster err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
14551b58561SPaul Burton if (err) {
1463eb99edbSMarkus Armbruster error_prepend(errp, "unable to read kernel load address from FIT: ");
14751b58561SPaul Burton ret = err;
14851b58561SPaul Burton goto out;
14951b58561SPaul Burton }
15051b58561SPaul Burton
1513eb99edbSMarkus Armbruster err = fit_image_addr(itb, img_off, "entry", &entry_addr, errp);
15251b58561SPaul Burton if (err) {
1533eb99edbSMarkus Armbruster error_prepend(errp, "unable to read kernel entry address from FIT: ");
15451b58561SPaul Burton ret = err;
15551b58561SPaul Burton goto out;
15651b58561SPaul Burton }
15751b58561SPaul Burton
15851b58561SPaul Burton if (ldr->kernel_filter) {
15951b58561SPaul Burton load_data = ldr->kernel_filter(opaque, data, &load_addr, &entry_addr);
16051b58561SPaul Burton }
16151b58561SPaul Burton
16251b58561SPaul Burton if (pend) {
16351b58561SPaul Burton *pend = load_addr + sz;
16451b58561SPaul Burton }
16551b58561SPaul Burton
16651b58561SPaul Burton load_addr = ldr->addr_to_phys(opaque, load_addr);
16751b58561SPaul Burton rom_add_blob_fixed(name, load_data, sz, load_addr);
16851b58561SPaul Burton
16951b58561SPaul Burton ret = 0;
17051b58561SPaul Burton out:
17151b58561SPaul Burton g_free((void *) data);
17251b58561SPaul Burton if (data != load_data) {
17351b58561SPaul Burton g_free((void *) load_data);
17451b58561SPaul Burton }
17551b58561SPaul Burton return ret;
17651b58561SPaul Burton }
17751b58561SPaul Burton
fit_load_fdt(const struct fit_loader * ldr,const void * itb,int cfg,void * opaque,const void * match_data,hwaddr kernel_end,Error ** errp)17851b58561SPaul Burton static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
17951b58561SPaul Burton int cfg, void *opaque, const void *match_data,
1803eb99edbSMarkus Armbruster hwaddr kernel_end, Error **errp)
18151b58561SPaul Burton {
182bfd65b4bSZhao Liu ERRP_GUARD();
183bc0e339bSMarkus Armbruster Error *err = NULL;
18451b58561SPaul Burton const char *name;
18551b58561SPaul Burton const void *data;
18651b58561SPaul Burton const void *load_data;
18751b58561SPaul Burton hwaddr load_addr;
188bc0e339bSMarkus Armbruster int img_off;
18951b58561SPaul Burton size_t sz;
19051b58561SPaul Burton int ret;
19151b58561SPaul Burton
19251b58561SPaul Burton name = fdt_getprop(itb, cfg, "fdt", NULL);
19351b58561SPaul Burton if (!name) {
19451b58561SPaul Burton return 0;
19551b58561SPaul Burton }
19651b58561SPaul Burton
1973eb99edbSMarkus Armbruster load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
19851b58561SPaul Burton if (!data) {
1993eb99edbSMarkus Armbruster error_prepend(errp, "unable to load FDT image from FIT: ");
20051b58561SPaul Burton return -EINVAL;
20151b58561SPaul Burton }
20251b58561SPaul Burton
203bc0e339bSMarkus Armbruster ret = fit_image_addr(itb, img_off, "load", &load_addr, &err);
204bc0e339bSMarkus Armbruster if (ret == -ENOENT) {
205d23b6caaSPhilippe Mathieu-Daudé load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
206bc0e339bSMarkus Armbruster error_free(err);
207bc0e339bSMarkus Armbruster } else if (ret) {
208bc0e339bSMarkus Armbruster error_propagate_prepend(errp, err,
209bc0e339bSMarkus Armbruster "unable to read FDT load address from FIT: ");
21051b58561SPaul Burton goto out;
21151b58561SPaul Burton }
21251b58561SPaul Burton
21351b58561SPaul Burton if (ldr->fdt_filter) {
21451b58561SPaul Burton load_data = ldr->fdt_filter(opaque, data, match_data, &load_addr);
21551b58561SPaul Burton }
21651b58561SPaul Burton
21751b58561SPaul Burton load_addr = ldr->addr_to_phys(opaque, load_addr);
21851b58561SPaul Burton sz = fdt_totalsize(load_data);
21951b58561SPaul Burton rom_add_blob_fixed(name, load_data, sz, load_addr);
22051b58561SPaul Burton
22151b58561SPaul Burton ret = 0;
22251b58561SPaul Burton out:
22351b58561SPaul Burton g_free((void *) data);
22451b58561SPaul Burton if (data != load_data) {
22551b58561SPaul Burton g_free((void *) load_data);
22651b58561SPaul Burton }
22751b58561SPaul Burton return ret;
22851b58561SPaul Burton }
22951b58561SPaul Burton
fit_cfg_compatible(const void * itb,int cfg,const char * compat)23051b58561SPaul Burton static bool fit_cfg_compatible(const void *itb, int cfg, const char *compat)
23151b58561SPaul Burton {
23251b58561SPaul Burton const void *fdt;
23351b58561SPaul Burton const char *fdt_name;
23451b58561SPaul Burton bool ret;
23551b58561SPaul Burton
23651b58561SPaul Burton fdt_name = fdt_getprop(itb, cfg, "fdt", NULL);
23751b58561SPaul Burton if (!fdt_name) {
23851b58561SPaul Burton return false;
23951b58561SPaul Burton }
24051b58561SPaul Burton
2413eb99edbSMarkus Armbruster fdt = fit_load_image_alloc(itb, fdt_name, NULL, NULL, NULL);
24251b58561SPaul Burton if (!fdt) {
24351b58561SPaul Burton return false;
24451b58561SPaul Burton }
24551b58561SPaul Burton
24651b58561SPaul Burton if (fdt_check_header(fdt)) {
24751b58561SPaul Burton ret = false;
24851b58561SPaul Burton goto out;
24951b58561SPaul Burton }
25051b58561SPaul Burton
25151b58561SPaul Burton if (fdt_node_check_compatible(fdt, 0, compat)) {
25251b58561SPaul Burton ret = false;
25351b58561SPaul Burton goto out;
25451b58561SPaul Burton }
25551b58561SPaul Burton
25651b58561SPaul Burton ret = true;
25751b58561SPaul Burton out:
25851b58561SPaul Burton g_free((void *) fdt);
25951b58561SPaul Burton return ret;
26051b58561SPaul Burton }
26151b58561SPaul Burton
load_fit(const struct fit_loader * ldr,const char * filename,void * opaque)26251b58561SPaul Burton int load_fit(const struct fit_loader *ldr, const char *filename, void *opaque)
26351b58561SPaul Burton {
2643eb99edbSMarkus Armbruster Error *err = NULL;
26551b58561SPaul Burton const struct fit_loader_match *match;
26651b58561SPaul Burton const void *itb, *match_data = NULL;
26751b58561SPaul Burton const char *def_cfg_name;
26851b58561SPaul Burton char path[FIT_LOADER_MAX_PATH];
2693eb99edbSMarkus Armbruster int itb_size, configs, cfg_off, off;
270*f5fcc648SAlex Bennée hwaddr kernel_end = 0;
27151b58561SPaul Burton int ret;
27251b58561SPaul Burton
27351b58561SPaul Burton itb = load_device_tree(filename, &itb_size);
27451b58561SPaul Burton if (!itb) {
27551b58561SPaul Burton return -EINVAL;
27651b58561SPaul Burton }
27751b58561SPaul Burton
27851b58561SPaul Burton configs = fdt_path_offset(itb, "/configurations");
27951b58561SPaul Burton if (configs < 0) {
2803eb99edbSMarkus Armbruster error_report("can't find node /configurations");
28151b58561SPaul Burton ret = configs;
28251b58561SPaul Burton goto out;
28351b58561SPaul Burton }
28451b58561SPaul Burton
28551b58561SPaul Burton cfg_off = -FDT_ERR_NOTFOUND;
28651b58561SPaul Burton
28751b58561SPaul Burton if (ldr->matches) {
28851b58561SPaul Burton for (match = ldr->matches; match->compatible; match++) {
28951b58561SPaul Burton off = fdt_first_subnode(itb, configs);
29051b58561SPaul Burton while (off >= 0) {
29151b58561SPaul Burton if (fit_cfg_compatible(itb, off, match->compatible)) {
29251b58561SPaul Burton cfg_off = off;
29351b58561SPaul Burton match_data = match->data;
29451b58561SPaul Burton break;
29551b58561SPaul Burton }
29651b58561SPaul Burton
29751b58561SPaul Burton off = fdt_next_subnode(itb, off);
29851b58561SPaul Burton }
29951b58561SPaul Burton
30051b58561SPaul Burton if (cfg_off >= 0) {
30151b58561SPaul Burton break;
30251b58561SPaul Burton }
30351b58561SPaul Burton }
30451b58561SPaul Burton }
30551b58561SPaul Burton
30651b58561SPaul Burton if (cfg_off < 0) {
30751b58561SPaul Burton def_cfg_name = fdt_getprop(itb, configs, "default", NULL);
30851b58561SPaul Burton if (def_cfg_name) {
30951b58561SPaul Burton snprintf(path, sizeof(path), "/configurations/%s", def_cfg_name);
31051b58561SPaul Burton cfg_off = fdt_path_offset(itb, path);
31151b58561SPaul Burton }
31251b58561SPaul Burton }
31351b58561SPaul Burton
31451b58561SPaul Burton if (cfg_off < 0) {
3153eb99edbSMarkus Armbruster error_report("can't find configuration");
31651b58561SPaul Burton ret = cfg_off;
31751b58561SPaul Burton goto out;
31851b58561SPaul Burton }
31951b58561SPaul Burton
3203eb99edbSMarkus Armbruster ret = fit_load_kernel(ldr, itb, cfg_off, opaque, &kernel_end, &err);
3213eb99edbSMarkus Armbruster if (ret) {
3223eb99edbSMarkus Armbruster error_report_err(err);
32351b58561SPaul Burton goto out;
32451b58561SPaul Burton }
32551b58561SPaul Burton
3263eb99edbSMarkus Armbruster ret = fit_load_fdt(ldr, itb, cfg_off, opaque, match_data, kernel_end,
3273eb99edbSMarkus Armbruster &err);
3283eb99edbSMarkus Armbruster if (ret) {
3293eb99edbSMarkus Armbruster error_report_err(err);
33051b58561SPaul Burton goto out;
33151b58561SPaul Burton }
33251b58561SPaul Burton
33351b58561SPaul Burton ret = 0;
33451b58561SPaul Burton out:
33551b58561SPaul Burton g_free((void *) itb);
33651b58561SPaul Burton return ret;
33751b58561SPaul Burton }
338