2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/types.h>
24 #include <libubox/ulog.h>
26 #include "libubi/libubi-tiny.h"
28 static int print_usage(void)
31 printf("ubi detach kernel|rootfs\n");
32 printf("ubi kernel <image.kernel.ubi>\n");
33 printf("ubi rootfs <image.rootfs.ubi>\n");
34 printf("ubi overlay <image.rootfs-overlay.ubi>\n");
39 static int mtd_find_index(char *name
)
41 FILE *fp
= fopen("/proc/mtd", "r");
48 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
49 if (strstr(line
, name
)) {
50 char *eol
= strstr(line
, ":");
68 static int mtd_find(char *name
, char *ret
)
70 int index
= mtd_find_index(name
);
74 sprintf(ret
, "/dev/mtd%d", index
);
79 static int ubi_find(libubi_t libubi
, char *name
, char *ret
)
81 int index
= mtd_find_index(name
);
84 while (ubi_dev_present(libubi
, ubi
))
86 struct ubi_dev_info info
;
88 if (ubi_get_dev_info1(libubi
, ubi
++, &info
))
91 if (info
.mtd_num
!= index
)
94 sprintf(ret
, "/dev/ubi%d", info
.dev_num
);
102 static int volume_find(libubi_t libubi
, char *name
, char *ret
)
104 int index
= mtd_find_index(name
);
105 struct ubi_vol_info vol
;
111 if (mtd_num2ubi_dev(libubi
, index
, &ubi
)) {
112 ULOG_ERR("failed to get ubi node for %s\n", name
);
116 if (ubi_get_vol_info1_nm(libubi
, ubi
, name
, &vol
)) {
117 ULOG_ERR("failed to get ubi volume info for %s\n", name
);
121 sprintf(ret
, "/dev/ubi%d_%d", ubi
, vol
.vol_id
);
126 static int main_detach(char *type
)
132 if (!strcmp(type
, "kernel"))
133 err
= mtd_find("kernel_ubi", mtd
);
134 else if (!strcmp(type
, "rootfs"))
135 err
= mtd_find("rootfs_ubi", mtd
);
137 return print_usage();
140 ULOG_ERR("MTD partition '%s_ubi' not found\n", type
);
144 libubi
= libubi_open();
146 ULOG_ERR("cannot open libubi");
150 err
= ubidetach(libubi
, mtd
);
152 ULOG_ERR("cannot detach \"%s\"", mtd
);
159 static int main_image(char *partition
, char *image
, char *overlay
)
171 if (stat(image
, &s
)) {
172 ULOG_ERR("image not found %s\n", image
);
176 if (!strcmp(partition
, "kernel"))
177 err
= mtd_find("kernel", _part
);
179 err
= mtd_find("rootfs", _part
);
181 if (overlay
&& !mtd_find(overlay
, _data
))
184 libubi
= libubi_open();
186 ULOG_ERR("cannot open libubi");
190 if (!strcmp(partition
, "kernel"))
191 err
= mtd_find("kernel_ubi", mtd
);
193 err
= mtd_find("rootfs_ubi", mtd
);
195 ULOG_ERR("MTD partition '%s_ubi' not found\n", partition
);
199 if (!strcmp(partition
, "kernel"))
200 err
= ubi_find(libubi
, "kernel_ubi", node
);
202 err
= ubi_find(libubi
, "rootfs_ubi", node
);
204 ULOG_ERR("UBI volume '%s' not found\n", partition
);
208 err
= ubidetach(libubi
, mtd
);
210 ULOG_ERR("cannot detach \"%s\"", mtd
);
214 err
= ubiattach(libubi
, mtd
);
216 ULOG_ERR("cannot attach \"%s\"", mtd
);
221 err
= ubirmvol(libubi
, node
, overlay
);
223 ULOG_ERR("cannot remove \"%s\"", node
);
228 if (volume_find(libubi
, partition
, volume
) < 0) {
229 ULOG_ERR("UBI volume '%s' not found\n", partition
);
233 err
= ubirsvol(libubi
, node
, partition
, s
.st_size
);
235 ULOG_ERR("cannot resize \"%s\"", partition
);
239 err
= ubiupdatevol(libubi
, volume
, image
);
241 ULOG_ERR("cannot update \"%s\"", volume
);
246 err
= ubimkvol(libubi
, node
, overlay
, 1);
248 ULOG_ERR("cannot make \"%s\"", overlay
);
253 libubi_close(libubi
);
258 static int main_info(void)
260 struct ubi_info info
;
264 libubi
= libubi_open();
266 ULOG_ERR("cannot open libubi");
270 if (ubi_get_info(libubi
, &info
)) {
271 ULOG_ERR("failed to get info\n");
275 for (i
= info
.lowest_dev_num
; i
<= info
.highest_dev_num
; i
++) {
276 struct ubi_dev_info dinfo
;
280 sprintf(ubi
, "/dev/ubi%d", i
);
281 if (ubi_get_dev_info(libubi
, ubi
, &dinfo
))
283 printf("device - %s\n size: %lldBytes\n bad blocks: %d\n",
284 &ubi
[5], dinfo
.total_bytes
, dinfo
.bad_count
);
285 for (j
= dinfo
.lowest_vol_id
; j
<= dinfo
.highest_vol_id
; j
++) {
286 struct ubi_vol_info vinfo
;
288 sprintf(ubi
, "/dev/ubi%d_%d", i
, j
);
289 if (ubi_get_vol_info(libubi
, ubi
, &vinfo
))
291 printf(" volume - %s\n", &ubi
[5]);
292 printf("\tname: %s\n", vinfo
.name
);
293 printf("\tsize: %lld\n", vinfo
.data_bytes
);
297 libubi_close(libubi
);
302 int main(int argc
, char **argv
)
304 if (argc
> 1 && !strcmp(argv
[1], "info"))
308 return print_usage();
310 if (!strcmp(argv
[1], "kernel")) {
311 return main_image("kernel", argv
[2], NULL
);
313 } else if (!strcmp(argv
[1], "rootfs")) {
314 return main_image("rootfs", argv
[2], NULL
);
316 } else if (!strcmp(argv
[1], "overlay")) {
317 return main_image("rootfs", argv
[2], "rootfs_data");
319 } else if (!strcmp(argv
[1], "detach")) {
320 return main_detach(argv
[2]);