uboot-mediatek: fix determine the size of an uImage.FIT using 'imsz' or 'imszb'.
[openwrt/staging/jow.git] / package / boot / uboot-mediatek / patches / 200-cmd-add-imsz-and-imszb.patch
1 --- a/cmd/bootm.c
2 +++ b/cmd/bootm.c
3 @@ -259,6 +259,67 @@ U_BOOT_CMD(
4 /* iminfo - print header info for a requested image */
5 /*******************************************************************/
6 #if defined(CONFIG_CMD_IMI)
7 +#if defined(CONFIG_FIT)
8 +#define SECTOR_SHIFT 9
9 +static int image_totalsize(struct cmd_tbl *cmdtp, int flag, int argc,
10 + char *const argv[], short int in_blocks)
11 +{
12 + ulong addr;
13 + void *fit;
14 + int bsize, tsize;
15 + char buf[16];
16 +
17 + if (argc >= 2)
18 + addr = simple_strtoul(argv[1], NULL, 16);
19 + else
20 + addr = image_load_addr;
21 +
22 + fit = (void *)map_sysmem(addr, 0);
23 + tsize = fit_get_totalsize(fit);
24 + unmap_sysmem(fit);
25 + if (tsize == 0)
26 + return 1;
27 +
28 + bsize = (tsize >> SECTOR_SHIFT) + ((tsize & ((1 << SECTOR_SHIFT) - 1))?1:0);
29 +
30 + if (!in_blocks)
31 + snprintf(buf, sizeof(buf), "%x", tsize);
32 + else
33 + snprintf(buf, sizeof(buf), "%x", bsize);
34 +
35 + if (argc >= 3)
36 + return env_set(argv[2], buf);
37 + else
38 + printf("%s\n", buf);
39 +
40 + return 0;
41 +}
42 +
43 +static int do_imsz(struct cmd_tbl *cmdtp, int flag, int argc,
44 + char *const argv[])
45 +{
46 + return image_totalsize(cmdtp, flag, argc, argv, 0);
47 +}
48 +
49 +static int do_imszb(struct cmd_tbl *cmdtp, int flag, int argc,
50 + char *const argv[])
51 +{
52 + return image_totalsize(cmdtp, flag, argc, argv, 1);
53 +}
54 +
55 +U_BOOT_CMD(
56 + imsz, CONFIG_SYS_MAXARGS, 1, do_imsz,
57 + "get image total size (in bytes)",
58 + "addr [maxhdrlen] [varname]\n"
59 +);
60 +
61 +U_BOOT_CMD(
62 + imszb, CONFIG_SYS_MAXARGS, 1, do_imszb,
63 + "get image total size (in blocks)",
64 + "addr [maxhdrlen] [varname]\n"
65 +);
66 +
67 +#endif
68 static int do_iminfo(struct cmd_tbl *cmdtp, int flag, int argc,
69 char *const argv[])
70 {
71 --- a/boot/image-fit.c
72 +++ b/boot/image-fit.c
73 @@ -2051,6 +2051,47 @@ static const char *fit_get_image_type_pr
74 return "unknown";
75 }
76
77 +size_t fit_get_totalsize(const void *fit)
78 +{
79 + int ret, ndepth, noffset, images_noffset;
80 + size_t data_size, hdrsize, img_total, max_size = 0;
81 + const void *data;
82 +
83 + ret = fdt_check_header(fit);
84 + if (ret) {
85 + debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
86 + ret);
87 + return 0;
88 + }
89 +
90 + hdrsize = fdt_totalsize(fit);
91 +
92 + /* take care of simple FIT with internal images */
93 + max_size = hdrsize;
94 +
95 + images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
96 + if (images_noffset < 0)
97 + goto out;
98 +
99 + for (ndepth = 0,
100 + noffset = fdt_next_node(fit, images_noffset, &ndepth);
101 + (noffset >= 0) && (ndepth > 0);
102 + noffset = fdt_next_node(fit, noffset, &ndepth)) {
103 + if (ndepth == 1) {
104 + ret = fit_image_get_data_and_size(fit, noffset, &data, &data_size);
105 + if (ret)
106 + goto out;
107 +
108 + img_total = data_size + (data - fit);
109 +
110 + max_size = (max_size > img_total) ? max_size : img_total;
111 + }
112 + }
113 +
114 +out:
115 + return max_size;
116 +}
117 +
118 int fit_image_load(struct bootm_headers *images, ulong addr,
119 const char **fit_unamep, const char **fit_uname_configp,
120 int arch, int ph_type, int bootstage_id,
121 --- a/include/image.h
122 +++ b/include/image.h
123 @@ -1047,6 +1047,7 @@ int fit_parse_subimage(const char *spec,
124 ulong *addr, const char **image_name);
125
126 int fit_get_subimage_count(const void *fit, int images_noffset);
127 +size_t fit_get_totalsize(const void *fit);
128 void fit_print_contents(const void *fit);
129 void fit_image_print(const void *fit, int noffset, const char *p);
130