在線升級uboot,內核和文件系統

在線升級uboot,內核和文件系統
fulinux

下面我在fl2440開發板上運行正常的情況下實現更新或升級uboot,內核和文件系統的任務。

如下是一個在線升級的腳本:

#!/bin/sh

#This shell scripts used to update the u-boot linux kernel, root file system image when Linux running

erase_cmd=flash_eraseall

write_cmd=nandwrite

cu_version=`cat /proc/version`

PROG_NAME=`basename $0`

IMAGE_TYPE=

ROOTFS_TYPE=

usage()

{

  echo "  Usage:   $PROG_NAME -[f/k/u/h] [filename]"

  echo "Example:   $PROG_NAME  linuxrom-s3c2440.bin"

  echo "           $PROG_NAME  u-boot-s3c2440.bin"

  echo "           $PROG_NAME  rootfs.jffs2"

  echo "           $PROG_NAME -u uboot.bin"

  echo "           $PROG_NAME -k lin.bin"

  echo "           $PROG_NAME -f fs.yaffs2"

  echo "           $PROG_NAME -h" 

  exit;

}

burn_image()

{

    partition=$1

    file_name=$2

    if ( ! $erase_cmd /dev/$partition) ; then

        echo "Erase /dev/$partition failure."

        exit

    fi

    if ( ! $write_cmd -p /dev/$partition $file_name) ; then

        echo "Write $file_name to /dev/$partition failure."

        exit

    fi

}

check_and_umount()

{

    MTD=$1

    MTDBLOCK=mtdblock${MTD:3}

    MOUNT_BLOCK=`cat /proc/mounts | grep $MTDBLOCK|awk -F " " '{print $1}'`

    if [ -n "$MOUNT_BLOCK" ] ; then

        umount /dev/$MTDBLOCK

    else

        echo "No need umount $MTDBLOCK"

    fi

}

check_image_type()

{

    IMAGE_NAME=$1

    if echo $IMAGE_NAME | grep -E "boot|uboot|u-boot|bootloader" > /dev/null ; then

        IMAGE_TYPE=BOOTLOADER

    elif echo $IMAGE_NAME | grep -E "linux|kernel" > /dev/null ; then

        IMAGE_TYPE=KERNEL

    elif  echo $IMAGE_NAME | grep -E "rootfs|jffs2|yaffs2|ubifs|cramfs|ramdisk" > /dev/null ; then

        IMAGE_TYPE=ROOTFS

    else

        IMAGE_TYPE=UNKNOW

    fi

}

up_bootloader()

{

    IMAGE_FILE=$1

    echo "Upgrade bootloader image '$IMAGE_FILE'"

    #To-Do: Find the mtd here, only do upgrade if we can find it, or throw error and exit out

    #echo $mtd | grep -E "u-boot|uboot" | awk -F ":" '{print $1}'

    partition=`cat /proc/mtd | grep -E "uboot|u-boot|U-boot|bootloader" | awk -F ":" '{print $1}'`

    if [ -z $partition ] ; then

        echo "Can not find the u-boot partition for update!"

        exit

    fi

    #To-Do: Start to burn the image to corresponding partition here

    burn_image $partition $IMAGE_FILE

}

up_kernel()

{

    IMAGE_FILE=$1

    echo "Upgrade linux kernel image '$IMAGE_FILE'"

    #To-Do: Find the mtd here, only do upgrade if we can find it, or throw error and exit out

    #echo $mtd | grep -E "linux|kernel" | awk -F ":" '{print $1}'

    partition=`cat /proc/mtd | grep -E "linux|kernel" | awk -F ":" '{print $1}'`

    if [ -z $partition ] ; then

        echo "Can not find the kernel partition for update!"

        exit

    fi

    #To-Do: Start to burn the image to corresponding partition here

    burn_image $partition $IMAGE_FILE

}

up_rootfs()

{

    IMAGE_NAME=$1

    ROOTFS_TYPE=${IMAGE_NAME##*.}

    VALID_ROOTFS_TYPE=0

    echo $ROOTFS_TYPE

    for i in jffs2 yaffs2 ubifs ramdisk cramfs ; do

        if [ "$i" = "$ROOTFS_TYPE" ] ; then

            VALID_ROOTFS_TYPE=1

            break;

        fi

    done

    if [ 0 == $VALID_ROOTFS_TYPE ] ; then

        echo "============================================================================================"

        echo "ERROR: Unknow rootfs image '$IMAGE_NAME', suffix/type: .$ROOTFS_TYPE"

        echo "The suffix of rootfs image file name should be: .ramdisk .yaffs2 .jffs2 .ubifs .cramfs"

        echo "============================================================================================"

        usage

    fi

    echo "Upgrade $ROOTFS_TYPE rootfs image '$IMAGE_FILE'"

    #To-Do: Find the mtd here, only do upgrade if we can find it, or throw error and exit out

    MTD=`cat /proc/mtd | grep -E "$ROOTFS_TYPE" | awk -F ":" '{print $1}'`

    #To-Do: Check this partition already mounted or not, if mounted then umount it first here

    check_and_umount $MTD

    #To-Do: Start to burn the image to corresponding partition here                                                                                                                    

    burn_image $MTD $IMAGE_FILE

}

while getopts "afku" opt

do

   case $opt in

      a)

           IMAGE_TYPE=APPS

           shift 1

           break;

           ;;

      k)

           IMAGE_TYPE=KERNEL

           shift 1

           break;

           ;;

      f)

           IMAGE_TYPE=ROOTFS

           shift 1

           break;

           ;;

      u)

           IMAGE_TYPE=BOOTLOADER

           shift 1

           break;

           ;;

      h)

           usage

           ;;

      ?)

           usage

           ;;

   esac

done

IMAGE_FILE=$1

if [ ! -n "$IMAGE_FILE" ] ; then

    usage

fi

if [ ! -n "$IMAGE_TYPE" ] ; then

    check_image_type  $IMAGE_FILE

fi

if [ $IMAGE_TYPE == BOOTLOADER ] ; then

    up_bootloader $IMAGE_FILE

elif [ $IMAGE_TYPE == KERNEL ] ; then

    up_kernel $IMAGE_FILE

elif [ $IMAGE_TYPE == ROOTFS ] ; then

    echo "$IMAGE_FILE ***************"

    up_rootfs $IMAGE_FILE

else

    echo "============================================================================================"

    echo "ERROR: Unknow image type: '$IMAGE_NAME'"

    echo "============================================================================================"

    usage

fi

文件系統如下:

[lingyun@localhost fulinux]$ ls

kernel  rootfs  systools  u-boot

[lingyun@localhost fulinux]$ cd rootfs/

[lingyun@localhost rootfs]$ ls

ramdisk-s3c2440.gz  rootfs  rootfs_tree.tar.bz2  tools

[lingyun@localhost rootfs]$ cd rootfs

[lingyun@localhost rootfs]$ pwd

/home/lingyun/fulinux/rootfs/rootfs

[lingyun@localhost rootfs]$ 

因爲上面的shell腳本需要用到兩個應用程序flash_eraseallnandwrite所以需要在上面的文件系統裏有這兩個程序。如果沒有添加方式如下:

[lingyun@localhost fulinux]$ ls

kernel  rootfs  systools  u-boot

[lingyun@localhost fulinux]$ cd systools/

[lingyun@localhost systools]$ ls

busybox

[lingyun@localhost systools]$ cd busybox/

[lingyun@localhost busybox]$ ls

build.sh  busybox-1.20.2  busybox-1.20.2.tar.bz2  patch

[lingyun@localhost busybox]$ cd busybox-1.20.2

[lingyun@localhost busybox-1.20.2]$ 

[lingyun@localhost busybox]$ ls

build.sh  busybox-1.20.2  busybox-1.20.2.tar.bz2  patch

[lingyun@localhost busybox]$ cd busybox-1.20.2

[lingyun@localhost busybox-1.20.2]$ vt100

[lingyun@localhost busybox-1.20.2]$ make menuconfig

主目錄:

Busybox Settings  --->

。。。

Installation Options ("make install" behavior)  --->

What kind of applet links to install (as soft-links)  --->

指定文件系統的目錄位置:

(/home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs) BusyBox installation prefix

。。。

回到主目錄:

Miscellaneous Utilities  ---> 

選擇如下的兩個程序:

。。。

[*] nandwrite

。。。

[*] flash_eraseall

。。。

保存退出。

[lingyun@localhost busybox-1.20.2]$ sudo make install

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/[ -> busybox

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/[[ -> busybox

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/addgroup -> busybox

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/adduser -> busybox

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/arping -> busybox

  /home/lingyun/fulinux/systools/busybox/../../rootfs/rootfs//bin/ash -> busybox

。。。。

[lingyun@localhost busybox-1.20.2]$ cd ~/fulinux/rootfs/rootfs

將腳本文件拷貝到tftp/目錄下:

[fulinux@centos6 tftp]$ ls

upgrade

下面將根目錄以jffs2文件系統形式移植到開發板中去,如下:

Copyright (C) 2013 fulinux <[email protected]>

root login: root

>: ls

apps     data     etc      init     linuxrc  proc     sbin     tmp      var

bin      dev      info     lib      mnt      root     sys      usr

>: 

tftp目錄下的upgrade文件下載到開發板中:

>: tftp -gr upgrade 192.168.1.3

upgrade              100% |*******************************|  5116   0:00:00 ETA

>: ls

apps     data     etc      init     linuxrc  proc     sbin     tmp      usr

bin      dev      info     lib      mnt      root     sys      upgrade  var

>: ll upgrade 

-rw-r--r--    1 root     root          5116 Feb 17 02:57 upgrade

>: chmod +x upgrade 

>: ll upgrade 

-rwxr-xr-x    1 root     root          5116 Feb 17 02:57 upgrade

>: 

將要升級或更新的uboot下載到開發板上,然後執行腳本升級uboot,如下:

>: tftp -gr u-boot-s3c2440.bin 192.168.1.3

u-boot-s3c2440.bin   100% |*******************************|   277k  0:00:00 ETA

>: ./upgrade

  Usage:   upgrade -[f/k/u/h] [filename]

Example:   upgrade  linuxrom-s3c2440.bin

           upgrade  u-boot-s3c2440.bin

           upgrade  rootfs.jffs2

           upgrade -u uboot.bin

           upgrade -k lin.bin

           upgrade -f fs.yaffs2

           upgrade -h

>: ./upgrade -u u-boot-s3c2440.bin 

Upgrade bootloader image 'u-boot-s3c2440.bin'

Erasing 128 Kibyte @ 100000 - 100% complete.

Writing at 0x00000000

Writing at 0x00020000

Writing at 0x00040000

>: reboot

重啓後如下:

。。。

root login: root

>: ls

apps                info                proc                u-boot-s3c2440.bin

bin                 init                root                upgrade

data                lib                 sbin                usr

dev                 linuxrc             sys                 var

etc                 mnt                 tmp

>: 

將要升級或更新的內核下載到開發板上,然後執行腳本升級內核,如下:

>: tftp -gr linuxrom-s3c2440.bin 192.168.1.3

linuxrom-s3c2440.bin 100% |*******************************|  2552k  0:00:00 ETA

>: sh upgrade

  Usage:   upgrade -[f/k/u/h] [filename]

Example:   upgrade  linuxrom-s3c2440.bin

           upgrade  u-boot-s3c2440.bin

           upgrade  rootfs.jffs2

           upgrade -u uboot.bin

           upgrade -k lin.bin

           upgrade -f fs.yaffs2

           upgrade -h

>: sh upgrade -k linuxrom-s3c2440.bin 

Upgrade linux kernel image 'linuxrom-s3c2440.bin'

Erasing 128 Kibyte @ f00000 - 100% complete.

Writing at 0x00000000

Writing at 0x00020000

Writing at 0x00040000

Writing at 0x00060000

Writing at 0x00080000

Writing at 0x000a0000

Writing at 0x000c0000

Writing at 0x000e0000

Writing at 0x00100000

Writing at 0x00120000

Writing at 0x00140000

Writing at 0x00160000

Writing at 0x00180000

Writing at 0x001a0000

Writing at 0x001c0000

Writing at 0x001e0000

Writing at 0x00200000

Writing at 0x00220000

Writing at 0x00240000

Writing at 0x00260000

>: reboot

重啓後如下:

Copyright (C) 2013 fulinux<[email protected]>

root login: root

>: ls

apps                  lib                   sys

bin                   linuxrc               tmp

data                  linuxrom-s3c2440.bin  u-boot-s3c2440.bin

dev                   mnt                   upgrade

etc                   proc                  usr

info                  root                  var

init                  sbin

>: 

將要升級或更新的文件系統下載到開發板上,然後執行腳本升級文件系統,下面以jffs2文件系統爲例:

話說,如果是initramfs文件系統的話就可以比較好的更新或是升級一個文件系統,但是我們這裏是用的jffs2,不能再jffs2分區裏把他擦了在跟新一個jffs2到這個分區中去,所以我們用另一種方式來達到這種更新文件系統效果。我們的根文件系統是出於mtdblock4分區中的,我們的mtdblock5分區原本是準備放yaffs2的,但是現在這個分區沒有任何文件系統,我們就將jffs2文件系統更新到這個分區中去,然後通過修改uboot中的bootargs參數就可以從mtdblock5分區啓動根文件系統了,具體操作如下:

~ >: tftp -gr rootfs.jffs2 192.168.1.3

rootfs.jffs2         100% |*******************************| 20480k  0:00:00 ETA

~ >: tftp -gr upgrade 192.168.1.3

upgrade              100% |*******************************|  5138   0:00:00 ETA

~ >: chmod +x upgrade 

~ >: cat /proc/mtd

dev:    size   erasesize  name

mtd0: 00100000 00020000 "mtdblock0 u-boot 1MB"

mtd1: 00f00000 00020000 "mtdblock1 kernel 15MB"

mtd2: 01400000 00020000 "mtdblock2 ramdisk 20MB"

mtd3: 01400000 00020000 "mtdblock3 cramfs 20MB"

mtd4: 01400000 00020000 "mtdblock4 jffs2 20MB"

mtd5: 02800000 00020000 "mtdblock5 yaffs2 40MB"

mtd6: 02800000 00020000 "mtdblock6 ubifs 40MB"

mtd7: 02800000 00020000 "mtdblock7 apps 40MB"

mtd8: 03c00000 00020000 "mtdblock8 data 40MB"

~ >: flash_eraseall /dev/mtd5

Erasing 128 Kibyte @ 2800000 - 100% complete.

~ >: nandwrite -p /dev/mtd5 rootfs.jffs2

Writing at 0x00000000

Writing at 0x00020000

Writing at 0x00040000

Writing at 0x00060000

Writing at 0x00080000

Writing at 0x000a0000

Writing at 0x000c0000

Writing at 0x000e0000

Writing at 0x00100000

Writing at 0x00120000

Writing at 0x00140000

Writing at 0x00160000

Writing at 0x00180000

Writing at 0x001a0000

Writing at 0x001c0000

Writing at 0x001e0000

Writing at 0x00200000

Writing at 0x00220000

Writing at 0x00240000

Writing at 0x00260000

Writing at 0x00280000

Writing at 0x002a0000

Writing at 0x002c0000

Writing at 0x002e0000

Writing at 0x00300000

Writing at 0x00320000

Writing at 0x00340000

Writing at 0x00360000

Writing at 0x00380000

Writing at 0x003a0000

Writing at 0x003c0000

Writing at 0x003e0000

Writing at 0x00400000

Writing at 0x00420000

Writing at 0x00440000

Writing at 0x00460000

Writing at 0x00480000

Writing at 0x004a0000

Writing at 0x004c0000

Writing at 0x004e0000

Writing at 0x00500000

Writing at 0x00520000

Writing at 0x00540000

Writing at 0x00560000

Writing at 0x00580000

Writing at 0x005a0000

Writing at 0x005c0000

Writing at 0x005e0000

Writing at 0x00600000

Writing at 0x00620000

Writing at 0x00640000

Writing at 0x00660000

Writing at 0x00680000

Writing at 0x006a0000

Writing at 0x006c0000

Writing at 0x006e0000

Writing at 0x00700000

Writing at 0x00720000

Writing at 0x00740000

Writing at 0x00760000

Writing at 0x00780000

Writing at 0x007a0000

Writing at 0x007c0000

Writing at 0x007e0000

Writing at 0x00800000

Writing at 0x00820000

Writing at 0x00840000

Writing at 0x00860000

Writing at 0x00880000

Writing at 0x008a0000

Writing at 0x008c0000

Writing at 0x008e0000

Writing at 0x00900000

Writing at 0x00920000

Writing at 0x00940000

Writing at 0x00960000

Writing at 0x00980000

Writing at 0x009a0000

Writing at 0x009c0000

Writing at 0x009e0000

Writing at 0x00a00000

Writing at 0x00a20000

Writing at 0x00a40000

Writing at 0x00a60000

Writing at 0x00a80000

Writing at 0x00aa0000

Writing at 0x00ac0000

Writing at 0x00ae0000

Writing at 0x00b00000

Writing at 0x00b20000

Writing at 0x00b40000

Writing at 0x00b60000

Writing at 0x00b80000

Writing at 0x00ba0000

Writing at 0x00bc0000

Writing at 0x00be0000

Writing at 0x00c00000

Writing at 0x00c20000

Writing at 0x00c40000

Writing at 0x00c60000

Writing at 0x00c80000

Writing at 0x00ca0000

Writing at 0x00cc0000

Writing at 0x00ce0000

Writing at 0x00d00000

Writing at 0x00d20000

Writing at 0x00d40000

Writing at 0x00d60000

Writing at 0x00d80000

Writing at 0x00da0000

Writing at 0x00dc0000

Writing at 0x00de0000

Writing at 0x00e00000

Writing at 0x00e20000

Writing at 0x00e40000

Writing at 0x00e60000

Writing at 0x00e80000

Writing at 0x00ea0000

Writing at 0x00ec0000

Writing at 0x00ee0000

Writing at 0x00f00000

Writing at 0x00f20000

Writing at 0x00f40000

Writing at 0x00f60000

Writing at 0x00f80000

Writing at 0x00fa0000

Writing at 0x00fc0000

Writing at 0x00fe0000

Writing at 0x01000000

Writing at 0x01020000

Writing at 0x01040000

Writing at 0x01060000

Writing at 0x01080000

Writing at 0x010a0000

Writing at 0x010c0000

Writing at 0x010e0000

Writing at 0x01100000

Writing at 0x01120000

Writing at 0x01140000

Writing at 0x01160000

Writing at 0x01180000

Writing at 0x011a0000

Writing at 0x011c0000

Writing at 0x011e0000

Writing at 0x01200000

Writing at 0x01220000

Writing at 0x01240000

Writing at 0x01260000

Writing at 0x01280000

Writing at 0x012a0000

Writing at 0x012c0000

Writing at 0x012e0000

Writing at 0x01300000

Writing at 0x01320000

Writing at 0x01340000

Writing at 0x01360000

Writing at 0x01380000

Writing at 0x013a0000

Writing at 0x013c0000

Writing at 0x013e0000

Writing at 0x01400000

~ >: reboot

重啓後設置bootargs參數,如下:

[ s3c2440@guowenxue ]# set bootargs 'noinitrd root=/dev/mtdblock5 rootfstype=jffs2 init=/linuxrc console=ttyS0,115200'

[ s3c2440@guowenxue ]# boot

就可啓動mtdblock5裏的文件系統了。

結束。

發佈了233 篇原創文章 · 獲贊 440 · 訪問量 105萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章