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 --- a/tools/env/Makefile
13 +++ b/tools/env/Makefile
14 @@ -24,6 +24,13 @@ ifeq ($(MTD_VERSION),old)
15 HOST_EXTRACFLAGS += -DMTD_OLD
19 +HOST_EXTRACFLAGS += -DUBI
20 +HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
22 +HOST_LOADLIBES = "-Wl,--gc-sections"
26 hostprogs-y := fw_printenv
28 --- a/tools/env/fw_env.c
29 +++ b/tools/env/fw_env.c
31 # include <mtd/mtd-user.h>
40 @@ -811,6 +814,11 @@ static int flash_write_buf (int dev, int
41 off_t top_of_range; /* end of the last block we may use */
42 loff_t blockstart; /* running start of the current block -
43 MEMGETBADBLOCK needs 64 bits */
45 + libubi_t *libubi = NULL;/* pointer to libubi struct */
47 + void *libubi = NULL;
52 @@ -916,7 +924,30 @@ static int flash_write_buf (int dev, int
56 - if (mtd_type != MTD_ABSENT) {
58 + if (mtd_type == MTD_UBIVOLUME) {
59 + struct ubi_vol_info volinfo;
60 + libubi = libubi_open();
62 + rc = ubi_get_vol_info(libubi,
63 + DEVNAME(dev_current), &volinfo);
64 + if (libubi && !rc) {
65 + erasesize = volinfo.leb_size;
66 + int leb = blockstart / erasesize;
67 + if (volinfo.type != UBI_STATIC_VOLUME)
68 + rc = ubi_leb_change_start(libubi, fd,
71 + rc = ubi_update_start(libubi, fd,
75 + libubi_close(libubi);
80 + if (!libubi && mtd_type != MTD_ABSENT) {
81 erase.start = blockstart;
82 ioctl(fd, MEMUNLOCK, &erase);
83 /* These do not need an explicit erase cycle */
84 @@ -933,7 +964,8 @@ static int flash_write_buf (int dev, int
86 "Seek error on %s: %s\n",
87 DEVNAME (dev), strerror (errno));
94 @@ -943,10 +975,11 @@ static int flash_write_buf (int dev, int
95 if (write (fd, data + processed, erasesize) != erasesize) {
96 fprintf (stderr, "Write error on %s: %s\n",
97 DEVNAME (dev), strerror (errno));
103 - if (mtd_type != MTD_ABSENT)
104 + if (!libubi && mtd_type != MTD_ABSENT)
105 ioctl(fd, MEMLOCK, &erase);
107 processed += erasesize;
108 @@ -957,6 +990,11 @@ static int flash_write_buf (int dev, int
109 if (write_total > count)
115 + libubi_close(libubi);
120 @@ -1068,12 +1106,8 @@ static int flash_read (int fd)
122 if (S_ISCHR(st.st_mode)) {
123 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
125 - fprintf(stderr, "Cannot get MTD information for %s\n",
126 - DEVNAME(dev_current));
129 - if (mtdinfo.type != MTD_NORFLASH &&
131 + mtdinfo.type != MTD_NORFLASH &&
132 mtdinfo.type != MTD_NANDFLASH &&
133 mtdinfo.type != MTD_DATAFLASH &&
134 mtdinfo.type != MTD_UBIVOLUME) {
135 @@ -1081,6 +1115,28 @@ static int flash_read (int fd)
136 mtdinfo.type, DEVNAME(dev_current));
142 + struct ubi_vol_info volinfo;
143 + libubi = libubi_open();
147 + rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
150 + libubi_close(libubi);
153 + memset(&mtdinfo, 0, sizeof(mtdinfo));
154 + mtdinfo.type = MTD_UBIVOLUME;
155 + mtdinfo.size = volinfo.data_bytes;
156 + mtdinfo.erasesize = volinfo.leb_size;
157 + mtdinfo.writesize = volinfo.leb_size;
158 + libubi_close(libubi);
162 memset(&mtdinfo, 0, sizeof(mtdinfo));
163 mtdinfo.type = MTD_ABSENT;