Skip to content

Instantly share code, notes, and snippets.

@rikka0w0 rikka0w0/sunxi_fel.md
Last active Feb 25, 2020

Embed
What would you like to do?
sunxi_fel.md

Mount a jffs2 image

http://wiki.emacinc.com/wiki/Mounting_JFFS2_Images_on_a_Linux_PC

#!/bin/bash

## Script to mount jffs2 filesystem using mtd kernel modules.
## EMAC, Inc. 2011

if [[ $# -lt 2 ]]
then
    echo "Usage: $0 FSNAME.JFFS2 MOUNTPOINT [ERASEBLOCK_SIZE]"
    exit 1
fi

if [ "$(whoami)" != "root" ]
then
    echo "$0 must be run as root!"
    exit 1
fi

if [[ ! -e $1 ]]
then
    echo "$1 does not exist"
    exit 1
fi

if [[ ! -d $2 ]]
then
    echo "$2 is not a valid mount point"
    exit 1
fi

if [[ "$3" == "" ]]
then
	esize="128"
else
	esize="$3"
fi

# cleanup if necessary
umount /tmp/mtdblock0 &>/dev/null
umount $2 &>/dev/null
modprobe -r jffs2 &>/dev/null
modprobe -r block2mtd &>/dev/null
modprobe -r mtdblock &>/dev/null
sleep 0.25
losetup -d /dev/loop1 &>/dev/null
sleep 0.25

modprobe loop || exit 1
losetup /dev/loop1 "$1" || exit 1
modprobe block2mtd || exit 1
modprobe jffs2 || exit 1
if [[ ! -e /tmp/mtdblock0 ]]
then
    mknod /tmp/mtdblock0 b 31 0 || exit 1
fi

echo "/dev/loop1,${esize}KiB" > /sys/module/block2mtd/parameters/block2mtd

mount -t jffs2 /tmp/mtdblock0 "$2" || exit 1

echo "Successfully mounted $1 on $2"
exit 0

sudo ./mountfs.sh rootfs.bin rootfs 64

Extract rootfs from offcial firmware

dd if=Nano_flash_480272.bin of=rootfs.img bs=1k skip=5184 status=progress
skip(blocks) = SPI_Flash_RootFS_Offset / blocksize = 0x510000 (byte) / 1024 (byte) = 5184

Duplicate the entire rootfs using tar:

# Mount the JFFS2
sudo ./mountfs.sh rootfs.suniv-f1c100s-musbbin rootfs 64

cd rootfs
tar cvzf ../rootfs.tar.gz .
cd ..
mkdir rootfs_new
cd rootfs_new
tar xvpzf ../rootfs.tar.gz

Pack the modified rootfs

mkfs.jffs2 -s 256b --pad=0xAF0000 -d rootfs_new/ -o rootfs_new.img

Write rootfs to flash

sudo ./sunxi-fel -p spiflash-write 0x0510000 rootfs_new.img
Write the content of rootfs_new.img to 0x0510000 and show progress

Write kernel and dtb

sudo sunxi-tools/sunxi-fel -p spiflash-write 0x0100000 linux/arch/arm/boot/dts/suniv-f1c100s-licheepi-nano.dtb
sudo sunxi-tools/sunxi-fel -p spiflash-write 0x0110000 linux/arch/arm/boot/zImage

In U-boot CMD, erase SPI flash and go to fel mode

sf probe 0;sf erase 0 0x100000;reset

Boot from RAM

sudo ./sunxi-fel -p -v uboot ~/licheepinano/uboot.bin \
             write 0x80008000 ../linux/arch/arm/boot/zImage  \
             write 0x80C00000 ../linux/arch/arm/boot/dts/suniv-f1c100s-licheepi-nano.dtb

bootm 0x80008000 - 0x80C00000

Compile Linux Kernel

make ARCH=arm menuconfig   #add bluethooth, etc.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j16
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j16 INSTALL_MOD_PATH=out modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j16 INSTALL_MOD_PATH=out modules_install

Make USB Host working

https://whycan.cn/t_1672.html I'm using Icenowy's 4.15 kernel

  1. Device Drivers > USB support > MUSB Mode Selection = Host only mode
  2. Device Drivers > USB support > USB Gadget Support ==> Clear this (Optional, to save some space)
  3. Edit:
--- ../linux/drivers/clk/sunxi-ng/ccu-suniv.c    2019-01-15 22:48:18.824587965 +0800
+++ drivers/clk/sunxi-ng/ccu-suniv.c    2019-01-23 09:05:17.959348454 +0800
@@ -238,7 +238,7 @@
/* The BSP header file has a CIR_CFG, but no mod clock uses this definition */

static SUNXI_CCU_GATE(usb_phy0_clk,    "usb-phy0",    "osc24M",
-              0x0cc, BIT(8), 0);
+              0x0cc, BIT(1), 0);

static SUNXI_CCU_GATE(dram_ve_clk,    "dram-ve",    "pll-ddr",
               0x100, BIT(0), 0);
  1. Edit dts: arch/arm/boot/dts/suniv-f1c100s-licheepi-nano.dts
&usb_otg {
        dr_mode = "otg";      // Change this from otg to host!
        status = "okay";
};
  1. Someone suggests to change this as well, but I find that changing this hangs the usb host operation:
drivers/usb/musb/sunxi.c: line 742
-      of_device_is_compatible(np, "allwinner,suniv-musb")) {
+      of_device_is_compatible(np, "allwinner,suniv-f1c100s-musb")) {

Uboot

git clone https://github.com/Lichee-Pi/u-boot.git -b nano-v2018.01
cd u-boot
make ARCH=arm licheepi_nano_spiflash_defconfig

make ARCH=arm menuconfig
# Edit the followings
# [*] Enable boot arguments
   = console=ttyS0,115200 panic=5 rootwait root=/dev/mtdblock3 rw rootfstype=jffs2
# [*] Enable a default value for bootcmd
   = sf probe 0 50000000; sf read 0x80C00000 0x100000 0x4000; sf read 0x80008000 0x110000 0x400000; bootz 0x80008000 - 0x80C00000

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
sudo ../sunxi-tools/sunxi-fel -p spiflash-write 0 u-boot-sunxi-with-spl.bin

References

https://wiki.dave.eu/index.php/Change_Linux_Command_Line_Parameter_from_U-boot http://bbs.lichee.pro/d/41-nano-spi-flash https://linux-sunxi.org/FEL/USBBoot

http://nano.lichee.pro/build_sys/kernel.html http://nano.lichee.pro/build_sys/build_flash.html http://nano.lichee.pro/step_by_step/two_sunxi-tools.html https://whycan.cn/t_2179_2.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.