Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477351
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-21 09:30
文章分类

全部博文(100)

文章存档

2017年(1)

2016年(16)

2015年(83)

我的朋友

分类: 嵌入式

2015-09-09 17:56:45

首先分析u-boot_movi.bin的组成,看uboot的makefile对u-boot.bin的处理:

点击(此处)折叠或打开

  1. # Always append ALL so that arch config.mk's can add custom ones
  2. ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND)

  3. all:        $(ALL)

  4. $(obj)u-boot.hex:    $(obj)u-boot
  5.         $(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@

  6. $(obj)u-boot.srec:    $(obj)u-boot
  7.         $(OBJCOPY) -O srec $< $@

  8. $(obj)u-boot.bin:    $(obj)u-boot
  9.         $(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

  10. #$(obj)u-boot_nand.bin:    $(obj)u-boot.bin    //生成u-boot.nand.bin
  11.         @cp -f u-boot.bin u-boot_nand.bin
  12.         chmod 777 u-boot_nand.bin

  13. #$(obj)u-boot_movi.bin:    $(obj)u-boot.bin
  14. # padding to 512k the u-boot.bin
  15.         @cat u-boot.bin > u-boot-2x.bin
  16.         @cat u-boot.bin >> u-boot-2x.bin                                    //合并2个u-boot.bin为  u-boot-2x.bin
  17.         @split -d -a 1 -b 512k u-boot-2x.bin u-boot-512k.bin//从u-boot-2x.bin分离出前512K,实际上= u-boot.bin + BL1,得到u-boot-512k.bin0

  18. # spiliting u-boot for BL1 (8kb)
  19.         @split -d -a 2 -b 8k u-boot.bin u-boot-8k.bin             //取出BL1,得到u-boot-8k.bin00

  20. # creating empty env space (16kb)
  21. #        @dd if=/dev/zero of=empty.env bs=16384 count=1 2> /dev/null  //16K空白的env

  22. # merging the bl1 and env with padded 512k binary
  23. #        @cat empty.env >> u-boot-512k.bin0                        //env追加到u-boot-512k.bin0,这句被屏蔽了,无效
  24.         @cat u-boot-8k.bin00 >> u-boot-512k.bin0                    //BL1(8k)追加到u-boot-512k.bin0

  25. # renaming and change mode
  26.         @mv u-boot-512k.bin0 u-boot_movi.bin                        //u-boot-512k.bin0改名为u-boot_movi.bin 
  27.         chmod 777 u-boot_movi.bin

  28. # removing temp files
  29.         @rm -f u-boot-8k*
  30.         @rm -f u-boot-512k*
  31.         @rm -f u-boot-2x*.bin
  32. #        @rm -f empty.env

  33. $(obj)u-boot_movi_256K.bin:    $(obj)u-boot.bin
  34. # padding to 512k the u-boot.bin
  35.         @cat u-boot.bin >> u-boot-2x.bin
  36.         @cat u-boot.bin >> u-boot-2x.bin
  37.         @split -d -a 1 -b 256k u-boot-2x.bin u-boot-512k.bin

  38. # spiliting u-boot for BL1 (8kb)
  39.         @split -d -a 2 -b 8k u-boot.bin u-boot-8k.bin

  40. # creating empty env space (16kb)
  41. #        @dd if=/dev/zero of=empty.env bs=16384 count=1 2> /dev/null

  42. # merging the bl1 and env with padded 512k binary
  43. #        @cat empty.env >> u-boot-512k.bin0
  44.         @cat u-boot-8k.bin00 >> u-boot-512k.bin0

  45. # renaming and change mode
  46.         @mv u-boot-512k.bin0 u-boot_movi.bin
  47.         chmod 777 u-boot_movi.bin

  48. # removing temp files
  49.         @rm -f u-boot-8k*
  50.         @rm -f u-boot-512k*
  51.         @rm -f u-boot-2x*.bin
  52. #        @rm -f empty.env
所以u-boot_movi.bin 的内容实际上为u-boot.bin(512K) + BL1(8K)
按照2416的要求
对于SD卡,uboot 的布局应该是BL2 + BL1(8K) + res(1K)。BL1的块位置为LAST - 18,按起始扇区从0计数则为totalblk - 18。那么u-boot_movi.bin的BL1前面还有u-boot-512k.bin,共(1024个blk),所以u-boot_movi.bin的块位置应为totalblk - 18 - 1024
对于SDHC卡,uboot 的布局应该是BL2 + BL1(8K) + res(513K)。BL1的块位置为LAST - 1042。按起始扇区从0计数则为totalblk - 1042u-boot_movi.bin的BL1前面还有u-boot-512k.bin,共(1024个blk),所以u-boot_movi.bin的块位置应为totalblk - 1042 - 1024

知道了烧写的位置,编写脚本就很简单了,不过我写的脚本还不是很完善,有空再加上格式化SD卡的功能,因为AM335对有MBR的SD卡不支持,当我制作335的SD启动卡时就要对卡进行特殊格式化,去除MBR,回到2416又要恢复MBR,有空再修复335uboot的这个bug。


点击(此处)折叠或打开

  1. #!/bin/bash
  2. # Authors:
  3. # LT Thomas <ltjr@ti.com>
  4. # Chase Maupin
  5. # Franklin Cooper Jr.
  6. #
  7. # create-sdcard.sh v0.3

  8. check_for_sdcards()
  9. {
  10.         # find the avaible SD cards
  11.         ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-8`
  12.         PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''`
  13.         if [ "$PARTITION_TEST" = "" ]; then
  14.      echo -e "Please insert a SD card to continue\n"
  15.      while [ "$PARTITION_TEST" = "" ]; do
  16.          read -p "Type 'y' to re-detect the SD card or 'n' to exit the script: " REPLY
  17.          if [ "$REPLY" = 'n' ]; then
  18.          exit 1
  19.          fi
  20.          ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-8`
  21.          PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''`
  22.      done
  23.         fi
  24. }

  25. # find the avaible SD cards
  26. ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-9`
  27. if [ "$ROOTDRIVE" = "root" ]; then
  28.     ROOTDRIVE=`readlink /dev/root | cut -c1-3`
  29. else
  30.     ROOTDRIVE=`echo $ROOTDRIVE | cut -c1-3`
  31. fi

  32. PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''`

  33. # Check for available mounts
  34. check_for_sdcards

  35. echo -e "\nAvailible Drives to write images to: \n"
  36. echo "# major minor size name "
  37. cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''
  38. echo " "

  39. DEVICEDRIVENUMBER=
  40. while true;
  41. do
  42.     read -p 'Enter Device Number or 'n' to exit: ' DEVICEDRIVENUMBER
  43.     echo " "
  44.         if [ "$DEVICEDRIVENUMBER" = 'n' ]; then
  45.                 exit 1
  46.         fi

  47.         if [ "$DEVICEDRIVENUMBER" = "" ]; then
  48.                 # Check to see if there are any changes
  49.                 check_for_sdcards
  50.                 echo -e "These are the Drives available to write images to:"
  51.                 echo "# major minor size name "
  52.                 cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''
  53.                 echo " "
  54.                continue
  55.         fi

  56.     DEVICEDRIVENAME=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep "${DEVICEDRIVENUMBER}:" | awk '{print $5}'`
  57.     if [ -n "$DEVICEDRIVENAME" ]
  58.     then
  59.      DRIVE=/dev/$DEVICEDRIVENAME
  60.      DEVICESIZE=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep "${DEVICEDRIVENUMBER}:" | awk '{print $4}'`
  61.         break
  62.     else
  63.         echo -e "Invalid selection!"
  64.                 # Check to see if there are any changes
  65.                 check_for_sdcards
  66.                 echo -e "These are the only Drives available to write images to: \n"
  67.                 echo "# major minor size name "
  68.                 cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''
  69.                 echo " "
  70.     fi
  71. done

  72. echo "$DEVICEDRIVENAME was selected"

  73. #Check the size of disk to is under 2GB?
  74. #区分SD卡和SDHC
  75. SDTYPE=SD
  76. if [ $DEVICESIZE -gt 2097152 ] ; then
  77. SDTYPE=SDHC
  78. fi

  79. cat << EOM

  80. ################################################################################

  81.     Selected Device is $SDTYPE

  82. ################################################################################
  83. EOM
  84. echo ""

  85. DRIVE=/dev/$DEVICEDRIVENAME
  86. NUM_OF_DRIVES=`df | grep -c $DEVICEDRIVENAME`

  87. #区分sdx和mmcblkXpY
  88. # This if statement will determine if we have a mounted sdX or mmcblkX device.
  89. # If it is mmcblkX, then we need to set an extra char in the partition names, 'p',
  90. # to account for /dev/mmcblkXpY labled partitions.
  91. if [[ ${DEVICEDRIVENAME} =~ ^sd. ]]; then
  92.     echo "$DRIVE is an sdx device"
  93.     P=''
  94. else
  95.     echo "$DRIVE is an mmcblkx device"
  96.     P='p'
  97. fi
  98. echo " "

  99. #获取总扇区数目
  100. NUM_OF_TOLAL_SECTORS=0
  101. #NUM_OF_TOLAL_SECTORS=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep "${DEVICEDRIVENUMBER}:" | awk '{print $4}'`
  102. NUM_OF_TOLAL_SECTORS=`fdisk -l $DRIVE | grep '\\|\' | awk '{print $8}'`
  103. #计算BL2在SD卡中的偏移地址
  104. BL2_OFFSET=0
  105. if [ $SDTYPE = SDHC ] ; then
  106.     let BL2_OFFSET=NUM_OF_TOLAL_SECTORS-1042-1024
  107. else
  108.     let BL2_OFFSET=NUM_OF_TOLAL_SECTORS-18-1024
  109. fi
  110. echo DRIVE = $DRIVE
  111. echo " "
  112. echo NUM_OF_TOLAL_SECTORS = $NUM_OF_TOLAL_SECTORS
  113. echo " "
  114. echo BL2_OFFSET = $BL2_OFFSET
  115. echo " "
  116. dd bs=512 iflag=dsync oflag=dsync if=uboot2416_64M_movi.bin of=$DRIVE seek=$BL2_OFFSET
  117. #卸载DEVICEDRIVENAME磁盘的所有分区
这里有个小技巧,如何获得SD卡总扇区?我想到2种方法,推荐第一种使用截取变量的方法,因为第二种用到的sed是外部命令,严格来讲执行效率较低。

首先要知道,/proc/partitions和fdisk -l显示的blocks都是以KBytes为单位的。扇区=blocks*2

第一种方法,使用截取变量:
#!/bin/bash
input="255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors"
output=`echo ${input#*total} | awk '{print $1}'`
echo $output

第二种,使用sed,将total 之前的所有字符替换为空白:
input="255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors"
echo $input | sed -e 's/.*total \(.*\) sectors/\1/'
或者使用扩展正则ERE:   echo $input | sed -r -e 's/.*total (.*) sectors/\1/' 

echo "255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors" | sed -r -n  's/.*total (.*) sectors/\1/'p
注意-r一定要在-e的前面,因为后面的替换操作是-e选项执行的。

2015.10.21:
获取总扇区数的第三种方法(绝对准确):
由于fdisk-l会因为采用中文导致total变为中文的“总容量”,因为读取/proc/partitions才是最准确的:
NUM_OF_TOLAL_SECTORS=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep  "${DEVICEDRIVENUMBER}:" | awk '{print $4}'`*2 | bc
其中 $ROOTDRIVE=sda  ${DEVICEDRIVENUMBER}=1

注意shell解释器务必选择#!/bin/bash,否则有些命令会报错误,如echo -e, [  ] 等等
阅读(2071) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~