Advertisement
Sweetening

Backup and Restore NAND and original MTD partitions

Feb 11th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. =======================================================
  2. How to restore NAND in Linux (any distro)
  3. =======================================================
  4.  
  5. Warning: Don't do this if you have any bad block in these MTD partitions, especially mtd0 (check your dmesg). And also do this at your own risk, it's highly recommended that you have serial console attached to recover from any bad flash.
  6.  
  7. The original mtd0 was saved using the following nanddump command:
  8.  
  9. /tmp # ./nanddump -nf usb/mtd0 /dev/mtd0
  10. Block size 131072, page size 2048, OOB size 64
  11. Dumping data starting at 0x00000000 and ending at 0x00200000...
  12.  
  13. So in order to restore mtd0 back, you can use flash_erase and nandwrite. Below are actual flashing and output. Your output should look exactly like these. If not, don't reboot, and post the question here.
  14.  
  15. 1. make sure the mtparts are correct
  16. # fw_printenv mtdparts
  17. mtdparts=mtdparts=orion_nand:2M(u-boot),3M(uImage),3M(uImage2),8M(failsafe),112M(root)
  18. 2. Erase mtd0:
  19. #flash_erase /dev/mtd0 0 16
  20. Erase Total 16 Units
  21. Performing Flash Erase of length 131072 at offset 0x1e0000 done
  22. 3. Write mtd0 backup file back to NAND:
  23. # nandwrite -on /dev/mtd0 mtd0
  24. Writing data to block 0 at offset 0x0
  25. Writing data to block 1 at offset 0x20000
  26. Writing data to block 2 at offset 0x40000
  27. Writing data to block 3 at offset 0x60000
  28. Writing data to block 4 at offset 0x80000
  29. Writing data to block 5 at offset 0xa0000
  30. Writing data to block 6 at offset 0xc0000
  31. Writing data to block 7 at offset 0xe0000
  32. Writing data to block 8 at offset 0x100000
  33. Writing data to block 9 at offset 0x120000
  34. Writing data to block 10 at offset 0x140000
  35. Writing data to block 11 at offset 0x160000
  36. Writing data to block 12 at offset 0x180000
  37. Writing data to block 13 at offset 0x1a0000
  38. Writing data to block 14 at offset 0x1c0000
  39. Writing data to block 15 at offset 0x1e0000
  40.  
  41. If you need to restore mtd1 to mtd4, then calculate the number of blocks for each mtd (i.e. a block is 128K, 1M is 8 blocks).
  42.  
  43. NOTE:
  44.  
  45. The nanddump command above is to back up your own box and later restore it to the same box. To backup mtd0 (or other mtds), make it a little more robust so that it can be used in different boxes without problem (thanks restamp for suggestion), use the following format:
  46.  
  47. nanddump --noecc --omitoob -l 0x80000 -f mtd0 /dev/mtd0
  48.  
  49.  
  50. =======================================================
  51. How to restore NAND in Serial Console
  52. =======================================================
  53.  
  54.  
  55. Here is what to do if you've decided that you need to restore NAND mtd0 in serial console to go back to stock OS. Assuming only mtd0 was messed up (that's how it usually happens). If the entire NAND was messed up, then use this procedure to restore mtd0 and then calculate the offset and size of the rest of the other mtds. If you're not sure, pls post questions in this thread.
  56.  
  57. Caveats: This is something I would not recommend doing, unless you can no loger boot into Debian or Arch. Because If you can use kwboot to boot the box, then you can boot Debian or Arch quite easily, without doing this procedure. See the Debian kernel/rootfs thread for how to create the Debian 4.4 rootfs to use with this box.
  58.  
  59. The procedure below has a lot more risk of errors occurred than flashing NAND mtd0 from Linux command line (section A).
  60.  
  61. Prerequisites
  62.  
  63. r1. This procedure requires a working serial console connected to the Pogo V4. And knowledge how to use kwboot to boot the latest version of u-boot.
  64.  
  65. r2. Download the latest u-boot, and kwboot binary as described in the u-boot thread to the serial console server (where you run kwboot). Create a single partition, type Ext3, USB thumbdrive. Copy your backup mtd0 to this USB drive root folder. Plug this USB drive into the Pogo V4 top USB port.
  66.  
  67. Restore NAND mtd0
  68.  
  69. 1. Connect serial console and use kwboot to boot the Pogo V4. See davygravy instruction for more details. I'm assume that you already know how to use kwboot.
  70.  
  71. Run kwboot and interrupt serial console when you see the countdown:
  72. ./kwboot -t -B 115200 /dev/ttyUSB0 -b uboot.2016.05-tld-1.pogo_v4.mtd0.kwb -p
  73. Expected output
  74. Sending boot message. Please reboot the target...-
  75. Sending boot image...
  76. 0 % [......................................................................]
  77. <snip>
  78. 95 % [......................................................................]
  79. 97 % [......................................................................]
  80. 99 % [....................................]
  81. [Type Ctrl-\ + c to quit]
  82.  
  83. U-Boot 2016.05-tld-1 (Jun 12 2016 - 13:41:47 -0700)
  84. Pogoplug V4
  85.  
  86. SoC: Kirkwood 88F6192_A1
  87. DRAM: 128 MiB
  88. WARNING: Caches not enabled
  89. NAND: 128 MiB
  90. MMC: kwsdio: 0
  91. *** Warning - bad CRC, using default environment
  92.  
  93. In: serial
  94. Out: serial
  95. Err: serial
  96. Net: egiga0
  97. Hit any key to stop autoboot: 0
  98.  
  99. 2. Verify the mtdparts is correct for Pogo V4
  100.  
  101. mtdparts
  102. Expected output
  103.  
  104. device nand0 <orion_nand>, # parts = 5
  105. #: name size offset mask_flags
  106. 0: u-boot 0x00200000 0x00000000 0
  107. 1: uImage 0x00300000 0x00200000 0
  108. 2: uImage2 0x00300000 0x00500000 0
  109. 3: failsafe 0x00800000 0x00800000 0
  110. 4: root 0x07000000 0x01000000 0
  111.  
  112. active partition: nand0,2 - (uImage2) 0x00300000 @ 0x00500000
  113.  
  114. defaults:
  115. mtdids : none
  116. mtdparts: none
  117.  
  118. 3. Start the USB drive
  119.  
  120. usb start
  121. Expected output
  122.  
  123. starting USB...
  124. USB0: USB EHCI 1.00
  125. scanning bus 0 for devices... 2 USB Device(s) found
  126. scanning usb for storage devices... 1 Storage Device(s) found
  127.  
  128. 4. Load the backup mtd0. Here the stock mtd0 backup file name was mtd0.pogo_v4
  129.  
  130. ext2load usb 0:1 0x800000 /mtd0.pogo_v4
  131. Expected output
  132. 2097152 bytes read in 1318 ms (1.5 MiB/s)
  133.  
  134.  
  135. 5. Erase mtd0
  136.  
  137. nand erase 0x0 0x200000
  138. Expected output
  139. NAND erase: device 0 offset 0x0, size 0x200000
  140. Erasing at 0x1e0000 -- 100% complete.
  141. OK
  142.  
  143. 6. Flash mtd0.pogo_v4 to NAND mtd0
  144.  
  145. nand write 0x800000 0x0 0x200000
  146. Expected output
  147.  
  148. NAND write: device 0 offset 0x0, size 0x200000
  149. 2097152 bytes written: OK
  150.  
  151. 7. Reset the box to boot with stock u-boot
  152. reset
  153. Expected output:
  154.  
  155. The stock u-boot banner and other info. And then it will proceeed to boot stock OS.
  156.  
  157. DONE.
  158.  
  159.  
  160.  
  161. =======================================================
  162. Automation script not yet tested
  163. =======================================================
  164.  
  165. I have provided a script that automates this process below also
  166.  
  167. Warning: Automating firmware restoration and interactions with bootloaders carries significant risk. Incorrect use could permanently damage your device. This script is provided for educational purposes and should be thoroughly reviewed and tested on a case-by-case basis.
  168.  
  169. Prerequisites:
  170. expect is installed on your system.
  171. kwboot, and your serial communication tool (minicom, screen, or similar) are installed and configured.
  172. You have a known-good NAND backup.
  173. You are familiar with the commands and responses of your device's bootloader.
  174. Script Overview:
  175. Use expect to handle kwboot and wait for it to finish.
  176. Open a serial communication session with expect and execute commands to restore NAND.
  177. This script assumes familiarity with the specific bootloader commands and responses.
  178.  
  179.  
  180. #!/usr/bin/expect -f
  181.  
  182. set timeout -1
  183. set SERIAL_DEVICE "/dev/ttyUSB0"
  184. set BAUD_RATE "115200"
  185. set UBOOT_IMAGE "path/to/uboot.2016.05-tld-1.pogo_v4.mtd0.kwb"
  186. set MTD0_BACKUP_PATH "path/to/mtd0.pogo_v4"
  187.  
  188. # Start kwboot and wait for it to boot the device
  189. spawn kwboot -t -B $BAUD_RATE $SERIAL_DEVICE -b $UBOOT_IMAGE -p
  190. expect "Sending boot image..."
  191. expect "99 %"
  192. send -- "\r"
  193. expect eof
  194.  
  195. # Now interact with the serial console
  196. spawn minicom -D $SERIAL_DEVICE -b $BAUD_RATE
  197.  
  198. # Example of interacting with U-Boot or bootloader
  199. # Stop autoboot
  200. expect "Hit any key to stop autoboot:"
  201. send -- "\r"
  202.  
  203. # Check for the prompt, adjust based on your bootloader
  204. expect "Pogoplug>"
  205.  
  206. # Start the USB and load the backup to RAM, adjust commands to your environment
  207. send -- "usb start\r"
  208. expect "USB Device(s) found"
  209. send -- "ext2load usb 0:1 0x800000 /mtd0.pogo_v4\r"
  210. expect "bytes read"
  211.  
  212. # Erase and write NAND, adjust as necessary
  213. send -- "nand erase 0x0 0x200000\r"
  214. expect "OK"
  215. send -- "nand write 0x800000 0x0 0x200000\r"
  216. expect "OK"
  217.  
  218. # Finish and reboot, adjust if needed
  219. send -- "reset\r"
  220. expect eof
  221.  
  222. # Close the session
  223. send -- "\035\014"
  224. expect eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement