3990f7df37b34d855c431c8e7a63a78e75bd40bc
[openwrt/staging/dedeckeh.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,50 @@ 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 + /* simple FIT with internal images */
93 + if (hdrsize > 0x1000)
94 + return hdrsize;
95 +
96 + images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
97 + if (images_noffset < 0) {
98 + printf("Can't find images parent node '%s' (%s)\n",
99 + FIT_IMAGES_PATH, fdt_strerror(images_noffset));
100 + return 0;
101 + }
102 +
103 + for (ndepth = 0,
104 + noffset = fdt_next_node(fit, images_noffset, &ndepth);
105 + (noffset >= 0) && (ndepth > 0);
106 + noffset = fdt_next_node(fit, noffset, &ndepth)) {
107 + if (ndepth == 1) {
108 + ret = fit_image_get_data_and_size(fit, noffset, &data, &data_size);
109 + if (ret)
110 + return 0;
111 +
112 + img_total = data_size + (data - fit);
113 +
114 + max_size = (max_size > img_total) ? max_size : img_total;
115 + }
116 + }
117 +
118 + return max_size;
119 +}
120 +
121 int fit_image_load(struct bootm_headers *images, ulong addr,
122 const char **fit_unamep, const char **fit_uname_configp,
123 int arch, int ph_type, int bootstage_id,
124 --- a/include/image.h
125 +++ b/include/image.h
126 @@ -1042,6 +1042,7 @@ int fit_parse_subimage(const char *spec,
127 ulong *addr, const char **image_name);
128
129 int fit_get_subimage_count(const void *fit, int images_noffset);
130 +size_t fit_get_totalsize(const void *fit);
131 void fit_print_contents(const void *fit);
132 void fit_image_print(const void *fit, int noffset, const char *p);
133