1 From 6e2630a0fc872d0db34157972f6dc3941f6d66dd Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 19 May 2014 21:38:01 +0200
4 Subject: [PATCH] tools/env: add support for env in ubi volume chardev
6 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
8 tools/env/Makefile | 5 ++++
9 tools/env/fw_env.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
10 2 files changed, 71 insertions(+), 10 deletions(-)
12 diff --git a/tools/env/Makefile b/tools/env/Makefile
13 index f5368bc..526d44d 100644
14 --- a/tools/env/Makefile
15 +++ b/tools/env/Makefile
16 @@ -20,6 +20,11 @@ ifeq ($(MTD_VERSION),old)
17 HOST_EXTRACFLAGS += -DMTD_OLD
21 +HOST_EXTRACFLAGS += -DUBI
22 +HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
26 hostprogs-y := fw_printenv_unstripped
28 diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
29 index 30d5b03..2217a25 100644
30 --- a/tools/env/fw_env.c
31 +++ b/tools/env/fw_env.c
33 # include <mtd/mtd-user.h>
42 @@ -809,6 +812,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
43 off_t top_of_range; /* end of the last block we may use */
44 loff_t blockstart; /* running start of the current block -
45 MEMGETBADBLOCK needs 64 bits */
47 + libubi_t *libubi = NULL;/* pointer to libubi struct */
49 + void *libubi = NULL;
54 @@ -914,7 +922,30 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
58 - if (mtd_type != MTD_ABSENT) {
60 + if (mtd_type == MTD_UBIVOLUME) {
61 + struct ubi_vol_info volinfo;
62 + libubi = libubi_open();
64 + rc = ubi_get_vol_info(libubi,
65 + DEVNAME(dev_current), &volinfo);
66 + if (libubi && !rc) {
67 + erasesize = volinfo.leb_size;
68 + int leb = blockstart / erasesize;
69 + if (volinfo.type != UBI_STATIC_VOLUME)
70 + rc = ubi_leb_change_start(libubi, fd,
73 + rc = ubi_update_start(libubi, fd,
77 + libubi_close(libubi);
82 + if (!libubi && mtd_type != MTD_ABSENT) {
83 erase.start = blockstart;
84 ioctl(fd, MEMUNLOCK, &erase);
85 /* These do not need an explicit erase cycle */
86 @@ -931,7 +962,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
88 "Seek error on %s: %s\n",
89 DEVNAME (dev), strerror (errno));
96 @@ -941,10 +973,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
97 if (write (fd, data + processed, erasesize) != erasesize) {
98 fprintf (stderr, "Write error on %s: %s\n",
99 DEVNAME (dev), strerror (errno));
105 - if (mtd_type != MTD_ABSENT)
106 + if (!libubi && mtd_type != MTD_ABSENT)
107 ioctl(fd, MEMLOCK, &erase);
109 processed += erasesize;
110 @@ -955,6 +988,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
111 if (write_total > count)
117 + libubi_close(libubi);
122 @@ -1066,12 +1104,8 @@ static int flash_read (int fd)
124 if (S_ISCHR(st.st_mode)) {
125 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
127 - fprintf(stderr, "Cannot get MTD information for %s\n",
128 - DEVNAME(dev_current));
131 - if (mtdinfo.type != MTD_NORFLASH &&
133 + mtdinfo.type != MTD_NORFLASH &&
134 mtdinfo.type != MTD_NANDFLASH &&
135 mtdinfo.type != MTD_DATAFLASH &&
136 mtdinfo.type != MTD_UBIVOLUME) {
137 @@ -1079,6 +1113,28 @@ static int flash_read (int fd)
138 mtdinfo.type, DEVNAME(dev_current));
144 + struct ubi_vol_info volinfo;
145 + libubi = libubi_open();
149 + rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
152 + libubi_close(libubi);
155 + memset(&mtdinfo, 0, sizeof(mtdinfo));
156 + mtdinfo.type = MTD_UBIVOLUME;
157 + mtdinfo.size = volinfo.data_bytes;
158 + mtdinfo.erasesize = volinfo.leb_size;
159 + mtdinfo.writesize = volinfo.leb_size;
160 + libubi_close(libubi);
164 memset(&mtdinfo, 0, sizeof(mtdinfo));
165 mtdinfo.type = MTD_ABSENT;