# Flash memory architecture

**URL:** <https://forum.openwrt.org/t/flash-memory-architecture/59089>\
**Category:** Installing and Using OpenWrt\
**Created:** [March 31, 2020, 8:15pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089 "2020-03-31T20:15:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [March 31, 2020, 8:15pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/1 "2020-03-31T20:15:39Z")

</div>

When trying to install Samba on my Hnet HW-C108 travel router with OpenWRT 19.07.2 I unexpectedly ran out of space. It seems to me the device does not contain as much flash memory as claimed. I'm trying to understand the way the memory is used.

The device page claims the router comes with 16MB of flash memory. This is what I find in the kernel log at boot time:

```
[0.611747] spi spi0.0: force spi mode3
[0.625219] m25p80 spi0.0: s25fl064k (8192 Kbytes)
[0.630159] 4 fixed-partitions partitions found on MTD device spi0.0
[0.636667] Creating 4 MTD partitions on "spi0.0":
[0.641583] 0x000000000000-0x000000030000 : "u-boot"
[0.647561] 0x000000030000-0x000000040000 : "u-boot-env"
[0.653878] 0x000000040000-0x000000050000 : "factory"
[0.659822] 0x000000050000-0x000001000000 : "firmware"
[0.665140] mtd: partition "firmware" extends beyond the end of device "spi0.0" -- size truncated to 0x7b0000
[0.678996] 2 uimage-fw partitions found on MTD device firmware
[0.685113] Creating 2 MTD partitions on "firmware":
[0.690190] 0x000000000000-0x00000017df95 : "kernel"
[0.696115] 0x00000017df95-0x0000007b0000 : "rootfs"
[0.702024] mtd: device 5 (rootfs) set to be root filesystem
[0.709353] 1 squashfs-split partitions found on MTD device rootfs
[0.715749] 0x0000003e3000-0x0000007b0000 : "rootfs_data"

```

The sizes derived from the address ranges match the sizes of the MTDblock devices, with the peculiarity that the "firmware" partition seems to be sized for a 16MB device, but gets truncated according to a total 8MB device (note the kernel message):

```
   196608 mtdblock0 "u-boot"
    65536 mtdblock1 "u-boot-env"
    65536 mtdblock2 "factory"
  8060928 mtdblock3 "firmware"
  1564160 mtdblock4 |-> "kernel"
  6496256 mtdblock5 |-> "rootfs"
  3985408 mtdblock6 |->"rootfs_data"

```

The arrows denote a "comprises" relationship.

It seems there is only 4MB space rather than 12MB in the overlay filesystem ("rootfs\_data"), which matches the output of df:

```
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 2560 2560 0 100% /rom
tmpfs 30012 924 29088 3% /tmp
/dev/mtdblock6 3892 1800 2092 46% /overlay
overlayfs:/overlay 3892 1800 2092 46% /
tmpfs 512 0 512 0% /dev
/dev/mmcblk0p1 62342144 60384 62281760 0% /storage

```

Is there a way I could have misinterpreted this? Should I correct the value on the device page?

The output of df shows I have plenty of space on my SD card. I'm thinking of utilising some of it to make room on the overlay filesystem. What's the smartest way of doing that? Right now it's an exfat filesystem. I have refrained from converting it to ext2/3/4, as the required kernel driver and required utilities are MASSIVE: together they get close to 1MB.

---

<div class="post-metadata">

**Author:** ![juppin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/juppin/32/29489_2.png) [@juppin](https://forum.openwrt.org/u/juppin)\
**Post date:** [March 31, 2020, 8:19pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/2 "2020-03-31T20:19:05Z")

</div>

> [@perot](#):
>
> [0.625219] m25p80 spi0.0: s25fl064k (8192 Kbytes)

But there is only a single spi nor flash with 8mb detected.

I think there is a mistake in the wiki and in the initial device support commit message.

0x1000000 are also defined in the device tree if you sum up all partitions and this are exactly 8mb.

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [March 31, 2020, 8:27pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/3 "2020-03-31T20:27:22Z")

</div>

> [@juppin](#):
>
> 0x1000000 are also defined in the device tree if you sum up all partitions and this are exactly 8mb

I beg to differ. In my world, 0x1000000 is 16MB.

---

<div class="post-metadata">

**Author:** ![juppin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/juppin/32/29489_2.png) [@juppin](https://forum.openwrt.org/u/juppin)\
**Post date:** [March 31, 2020, 8:30pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/4 "2020-03-31T20:30:21Z")

</div>

You are absolutely right 🤫

---

<div class="post-metadata">

**Author:** ![juppin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/juppin/32/29489_2.png) [@juppin](https://forum.openwrt.org/u/juppin)\
**Post date:** [March 31, 2020, 8:35pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/5 "2020-03-31T20:35:29Z")

</div>

The firmware partition size in the current (trunk/master) device tree is correct!

> <https://github.com/openwrt/openwrt/blob/master/target/linux/ramips/dts/mt7620a_hnet_c108.dts#L149>

I think you have to open up your device and determine the real flash chip type.

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [March 31, 2020, 8:48pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/6 "2020-03-31T20:48:19Z")

</div>

> [@juppin](#):
>
> I think you have to open up your device and determine the real flash chip type.

Are you saying this may be a device tree bug, and if the device indeed has 16MB flash memory, fixing it would give me another 8MB?

Now that would be a reason to open it 🙂

PS.: In the meantime I checked the OpenWRT image that I flashed. It is indeed just 8MB in size.

---

<div class="post-metadata">

**Author:** ![juppin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/juppin/32/29489_2.png) [@juppin](https://forum.openwrt.org/u/juppin)\
**Post date:** [March 31, 2020, 8:55pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/7 "2020-03-31T20:55:57Z")

</div>

> [@perot](#):
>
> Are you saying this may be a device tree bug, and if the device indeed has 16MB flash memory, fixing it would give me another 8MB?

No, the compatible "jedec,spi-nor" instructs the kernel to auto detect the spi nor flash according to the jedec standard.  
As your flash chip is detected as "s25fl064k" instead of "en25q128" there are three reasons for your missing space.

1. Your device has really this detected spi nor type
2. The magic for your real flash chip does conflict with the one of "s25fl064k"
3. The magic for "s25fl064k" are wrong

> [@perot](#):
>
> In the meantime I checked the OpenWRT image that I flashed. It is indeed just 8MB in size.

The image hasn´t to be the same size as the space on the flash chip!

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [March 31, 2020, 9:36pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/8 "2020-03-31T21:36:27Z")

</div>

> [@juppin](#):
>
> The image hasn´t to be the same size as the space on the flash chip!

I know. But it is one of many hints that the 16MB was some initial mistake that never got corrected. After all, 8MB is also more than the actual data inside.

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [April 1, 2020, 8:39am UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/9 "2020-04-01T08:39:30Z")

</div>

This brings me to another question: The kernel provides device nodes to access the flash memory. There are (# for some decimal digits) /dev/mtd#, /dev/mtd#ro (both major number 90), and /dev/mtdblock# (major number 31), which for the same decimal digit (0 ... 6 in this device) refer to the same memory. The file 'device.txt' from the linux source describes very shortly the devices. The major number 90 ("Memory Technology Devices") is straightforward: mtd# is read/write, mtd#ro is read-only access. The description for major number 31 confuses me:

```
  31 block	ROM/flash memory card
		  0 = /dev/rom0 First ROM card (rw)
		      ...
		  7 = /dev/rom7 Eighth ROM card (rw)
		  8 = /dev/rrom0	First ROM card (ro)
		    ...
		 15 = /dev/rrom7	Eighth ROM card (ro)
		 16 = /dev/flash0	First flash memory card (rw)
		    ...
		 23 = /dev/flash7	Eighth flash memory card (rw)
		 24 = /dev/rflash0	First flash memory card (ro)
		    ...
		 31 = /dev/rflash7	Eighth flash memory card (ro)

		The read-write (rw) devices support back-caching
		written data in RAM, as well as writing to flash RAM
		devices. The read-only devices (ro) support reading
		only.

```

Now, what is a ROM with read/write access?

I was looking for a tool that can backup the _entire_ configuration, i.e. including software (installed packages). Basically what I want is a flash memory dump. Is there a special tool for backup/restore?

If not: Dumping via dd is easy, but the data may not be in a consistent state such that when restoring the router can boot again. For that I imagine I'd need to remount the overlay filesystem into ro mode. Is that sufficient for what I have in mind? Then, before restoring, I'd do the same (remount ro), then dd to the rw device, and reboot. Are there any obvious roadblocks to this approach?

---

<div class="post-metadata">

**Author:** ![juppin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/juppin/32/29489_2.png) [@juppin](https://forum.openwrt.org/u/juppin)\
**Post date:** [April 1, 2020, 9:21pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/10 "2020-04-01T21:21:43Z")

</div>

> [@perot](#):
>
> Is there a special tool for backup/restore?

Probably this is something for you:

> **[Generic NOR backup](https://openwrt.org/docs/guide-user/installation/generic.backup#generic_nor_backup)**
>
> Generic NOR backup \* This guide describes how to perform block-level backup/restore via CLI. \* Follow Backup and restore for file-level backup/restore. NAND-based devices should use NAND-aware utilities, as dd does not properly handle the error...

LuCI does also allow you to download tbe mtd partition contents.  
If im right under System -\> Backup / Restore

> [@perot](#):
>
> Dumping via dd is easy, but the data may not be in a consistent state such that when restoring the router can boot again. For that I imagine I'd need to remount the overlay filesystem into ro mode. Is that sufficient for what I have in mind?

From my point of view yes.

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [April 2, 2020, 10:33am UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/11 "2020-04-02T10:33:58Z")

</div>

> [@juppin](#):
>
> Probably this is something for you:  
> [https://openwrt.org/docs/guide-user/installation/generic.backup#generic\_nor\_backup](https://openwrt.org/docs/guide-user/installation/generic.backup#generic_nor_backup)

Thanks, looks like exactly what I was looking for. I will study it carefully.

> [@](#):
>
> LuCI does also allow you to download tbe mtd partition contents.  
> If im right under System -\> Backup / Restore

Aha, in fact I had not noticed it before. However, it says:

```plaintext
NOTE: THIS FEATURE IS FOR PROFESSIONALS!

```

I take that to mean that there are caveats involved that the user needs to be aware of, and the user should know exactly what LuCI is doing there in the background. Do you know of any docs related to this? An immediate question is, whether I get a pure image, or one that is tagged with info which block is backed up? I can check the size of course. But there is no specific option to restore an image, but only the sysupgrade option. How do I make a backup image `sysupgrade-compatible` (as the GUI calls it), such that sysupgrade knows which mtdblock to restore?

Maybe those questions are addressed in the document that you pointed me to. I will read that first.

---

<div class="post-metadata">

**Author:** ![perot](https://forum.openwrt.org/letter_avatar/perot/32/6_449c533f586939b6e5f246632a46421f.png) [@perot](https://forum.openwrt.org/u/perot)\
**Post date:** [April 2, 2020, 12:10pm UTC](https://forum.openwrt.org/t/flash-memory-architecture/59089/12 "2020-04-02T12:10:11Z")

</div>

> [@perot](#):
>
> Thanks, looks like exactly what I was looking for. I will study it carefully.

So these backup scripts do not remount the overlay ro. Seems they rely on the fact that all normal system activity happens in RAM filesystems, and only the user would initiate write activities (via LuCI/uCI, or calling opkg). Still the filesystem is mounted rw, and most filesystems modify it anyway even if nothing writes to it, if for simply flagging it for non-proper unmounting after power failure.

Also the scripts assume nor flash. In an earlier message you called my device's flash nor-flash as well:

> [@juppin](#):
>
> But there is only a single spi nor flash with 8mb detected.

How can you be sure? Is that coded in the line

```plaintext
[0.625219] m25p80 spi0.0: s25fl064k (8192 Kbytes)

```

?
