前兩篇介紹了uboot-2013.01的配置原理以及大體的運行流程,本文將討論如何對uboot源碼進行配置,將一個可用的uboot燒錄到SD卡中。
定製自己的core board
市面上能買到的開發板的核心板基本都是基於官方參考板製作的,所以雖然標準操作是”定製”自己的core board,但鑑於我的板子的核心板是基於三星的參考板做的,所以我們做的主要工作就是按照(一)中的原理,編寫(山寨)我們”自己的”核心板配置。我們需要的目錄是“board/samsung/origen/”,這部分的主要功能就是將其中的文件”改名字”。首先來認識一下原版以示尊重
board/samsung/origen/
$ls board/samsung/origen/
lowlevel_init.S Makefile mem_setup.S mmc_boot.c origen.c origen_setup.h tools
接下來就開始我們的山寨工作
//山寨參考板目錄
$cp -arf ./board/samsung/origen ./board/samsung/xboot
//山寨關鍵文件
$mv ./board/samsung/xboot/origen.c ./board/samsung/xboot/xboot.c
uboot的編譯系統和內核的類似,所以Makefile也得改(./board/samsung/xboot/Makefile)
from
30 ifndef CONFIG_SPL_BUILD
31 COBJS += origen.o
32 endif
to
30 ifndef CONFIG_SPL_BUILD
31 COBJS += xboot.o
32 endif
include/configs/origen.h
用於配置整個板子的頭文件也不能放過
$cp include/configs/origen.h include/configs/xboot.h
from
104 #define CONFIG_SYS_PROMPT “ORIGEN # ”
133 #define CONFIG_IDENT_STRING ” for ORIGEN”
to
104 #define CONFIG_SYS_PROMPT “xboot # ”
133 #define CONFIG_IDENT_STRING ” for xboot”
boards.cfg
最後,別忘了我在上文提到的boards.cfg文件,如果這裏面不動手腳,我們的板子是不會被Makefile找到的,So,
284 origen arm armv7 origen samsung exynos
285 xboot arm armv7 xboot samsung exynos
和之前一樣,至此,我們就可以先編譯一下過過手癮(順便檢查一下配置^-^),
1 #!/bin/bash
2 CPU_NUM= {newName}_config
8 make -j${CPU_NUM}
上面是編譯的小腳本,下面是編譯的輸出。
編譯and燒錄
按照(一)中介紹的,此時已經可以”make xboot_config;make -8”並得到uboot.bin,但此時這個uboot.bin是不能直接燒錄的。但無論如何,請暫且記住它的大小:176568byte。接下來,我們需要對uboot.bin進行一系列處理使它能夠在exynos4412上運行,這其中要用到下面的幾個命令或三星提供的工具軟件,這些操作的目的就是根據三星的芯片的啓動要求對uboot.bin 進行一些處理,包括在特定長度的位置加上和校驗信息以及插入一些文件段。
修改地方
這裏對原文中的uboot.bin打包動作,進行修改,使用三星的提供的源碼對其進行打包。一步到位,更加簡單:
源碼如下:
/*
* Copyright (c) 2010 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *fp;
unsigned char src;
char *Buf, *a;
int BufLen;
int nbytes, fileLen;
unsigned int checksum = 0;
int i;
if (argc != 4)
{
printf("Usage: mkbl1 <source file> <destination file> <size> \n");
return -1;
}
BufLen = atoi(argv[3]);
Buf = (char *)malloc(BufLen);
memset(Buf, 0x00, BufLen);
fp = fopen(argv[1], "rb");
if( fp == NULL)
{
printf("source file open error\n");
free(Buf);
return -1;
}
fseek(fp, 0L, SEEK_END);
fileLen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if ( BufLen > fileLen )
{
printf("Usage: unsupported size\n");
free(Buf);
fclose(fp);
return -1;
}
nbytes = fread(Buf, 1, BufLen, fp);
if ( nbytes != BufLen )
{
printf("source file read error\n");
free(Buf);
fclose(fp);
return -1;
}
fclose(fp);
for(i = 0;i < (14 * 1024) - 4;i++)
{
checksum += (unsigned char)(Buf[i]);
}
*(unsigned int*)(Buf+i) = checksum;
fp = fopen(argv[2], "wb");
if (fp == NULL)
{
printf("destination file open error\n");
free(Buf);
return -1;
}
a = Buf;
nbytes = fwrite( a, 1, BufLen, fp);
if ( nbytes != BufLen )
{
printf("destination file write error\n");
free(Buf);
fclose(fp);
return -1;
}
free(Buf);
fclose(fp);
return 0;
}
對其進行編譯,生成一個可執行文件 mkbl2, 在腳本中使用如下命令,就可以完成bin文件的打包任務:
####################################
# check files
E4412_UBOOT=../../u-boot.bin
MKBL2=../mkbl2
if [ ! -f ${E4412_UBOOT} ]; then
echo "Error: u-boot.bin NOT found, please build it & try again."
exit -1
fi
if [ ! -f ${MKBL2} ]; then
echo "Error: can not find host tool - mkbl2, stop."
exit -1
fi
#<make bl2> 對可執行文件(mkbl2)傳入參數
${MKBL2} ${E4412_UBOOT} bl2.bin 14336
最後,使用命令 ./sd_fusing.sh /dev/your_SD_device 即可完成燒寫的任務。
主要分爲 BL1, BL2, u-boot, TrustZone S/W 這四個區域的燒寫。
# the file for tiny4412 development board.
# file name: sd_fusing.sh
#
# Copyright (C) 2011 Samsung Electronics Co., Ltd.
# http://www.samsung.com/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
####################################
if [ -z $1 ]
then
echo "usage: ./sd_fusing.sh <SD Reader's device file>"
exit 0
fi
if [ -b $1 ]
then
echo "$1 reader is identified."
else
echo "$1 is NOT identified."
exit 0
fi
####################################
#<verify device>
BDEV_NAME=`basename $1`
BDEV_SIZE=`cat /sys/block/${BDEV_NAME}/size`
if [ ${BDEV_SIZE} -le 0 ]; then
echo "Error: NO media found in card reader."
exit 1
fi
if [ ${BDEV_SIZE} -gt 32000000 ]; then
echo "Error: Block device size (${BDEV_SIZE}) is too large"
exit 1
fi
####################################
# check files
E4412_UBOOT=../../u-boot.bin
MKBL2=../mkbl2
if [ ! -f ${E4412_UBOOT} ]; then
echo "Error: u-boot.bin NOT found, please build it & try again."
exit -1
fi
if [ ! -f ${MKBL2} ]; then
echo "Error: can not find host tool - mkbl2, stop."
exit -1
fi
#<make bl2>
${MKBL2} ${E4412_UBOOT} bl2.bin 14336
####################################
# fusing images
signed_bl1_position=1
bl2_position=17
uboot_position=49
tzsw_position=705
#<BL1 fusing>
echo "---------------------------------------"
echo "BL1 fusing"
dd iflag=dsync oflag=dsync if=./E4412_N.bl1.bin of=$1 seek=$signed_bl1_position
#<BL2 fusing>
echo "---------------------------------------"
echo "BL2 fusing"
dd iflag=dsync oflag=dsync if=./bl2.bin of=$1 seek=$bl2_position
#<u-boot fusing>
echo "---------------------------------------"
echo "u-boot fusing"
dd iflag=dsync oflag=dsync if=${E4412_UBOOT} of=$1 seek=$uboot_position
#<TrustZone S/W fusing>
echo "---------------------------------------"
echo "TrustZone S/W fusing"
dd iflag=dsync oflag=dsync if=./E4412_tzsw.bin of=$1 seek=$tzsw_position
#<flush to disk>
sync
####################################
#<Message Display>
echo "---------------------------------------"
echo "U-boot image is fused successfully."
echo "Eject SD card and insert it again."
致敬社區大神:原文鏈接