我的博客已迁移至自建服务器:博客传送门,CSDN博客暂时停止,如有机器学习方面的兴趣,欢迎来看一看。
此外目前我在gitHub上准备一些李航的《统计学习方法》的实现算法,目标将书内算法全部手打实现,欢迎参观并打星。GitHib传送门
到这里uboot差不多已经结束了,再完成最后一步,就能启动内核啦。
一. 首先先解决启动时的一个警告,这个和裁剪无关,只是先把警告消除掉
*** Warning - bad CRC, using default environment
这句话意思就是环境没有配置,搜索这句话是哪里打出来的
a.在env_common.c
文件内的下面这个函数
void set_default_env(const char *s)
{
if (sizeof(default_environment) > ENV_SIZE) {
puts("*** Error - default environment is too large\n\n");
return;
}
if (s) {
if (*s == '!') {
printf("*** Warning - %s, "
"using default environment\n\n",
s + 1);
} else {
puts(s);
}
} else {
puts("Using default environment\n\n");
}
if (himport_r(&env_htab, (char *)default_environment,
sizeof(default_environment), '\0', 0) == 0)
error("Environment import failed: errno = %d\n", errno);
gd->flags |= GD_FLG_ENV_READY;
}
进入default_environment
结构体看看
const uchar default_environment[] = {
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
#endif
#ifdef CONFIG_RAMBOOTCOMMAND
"ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
#endif
#ifdef CONFIG_NFSBOOTCOMMAND
"nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
#endif
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
"bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
#endif
#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
"baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
#endif
#ifdef CONFIG_LOADS_ECHO
"loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
#endif
#ifdef CONFIG_ETHADDR
"ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
#endif
#ifdef CONFIG_ETH1ADDR
"eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
#endif
#ifdef CONFIG_ETH2ADDR
"eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
#endif
#ifdef CONFIG_ETH3ADDR
"eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
#endif
#ifdef CONFIG_ETH4ADDR
"eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
#endif
#ifdef CONFIG_ETH5ADDR
"eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
#endif
#ifdef CONFIG_IPADDR
"ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
#endif
#ifdef CONFIG_SERVERIP
"serverip=" MK_STR(CONFIG_SERVERIP) "\0"
#endif
#ifdef CONFIG_SYS_AUTOLOAD
"autoload=" CONFIG_SYS_AUTOLOAD "\0"
#endif
#ifdef CONFIG_PREBOOT
"preboot=" CONFIG_PREBOOT "\0"
#endif
#ifdef CONFIG_ROOTPATH
"rootpath=" CONFIG_ROOTPATH "\0"
#endif
#ifdef CONFIG_GATEWAYIP
"gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
#endif
#ifdef CONFIG_NETMASK
"netmask=" MK_STR(CONFIG_NETMASK) "\0"
#endif
#ifdef CONFIG_HOSTNAME
"hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
#endif
#ifdef CONFIG_BOOTFILE
"bootfile=" CONFIG_BOOTFILE "\0"
#endif
#ifdef CONFIG_LOADADDR
"loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
#endif
#ifdef CONFIG_CLOCKS_IN_MHZ
"clocks_in_mhz=1\0"
#endif
#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
"pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
#endif
#ifdef CONFIG_EXTRA_ENV_SETTINGS
CONFIG_EXTRA_ENV_SETTINGS
#endif
"\0"
};
看到那么多宏定义,头都有点大了。
一个一个往下看,不用全部都看,平时用到的参数看一看,首先是
CONFIG_BOOTDELAY
这个是boot启动的时候那个倒计时,默认是5秒,我们可以改成3秒,也可以不改
#define CONFIG_BOOTDELAY 3 /* autoboot after 3 seconds */
然后是ip设置
"ipaddr=" MK_STR(CONFIG_IPADDR)
将宏修改成如下,可以随便设,只要和网内其他IP不冲突就行了
#define CONFIG_IPADDR 192.168.1.120
连带的一些都直接写出来吧,就不一个个说了,这些都挨个设置好
#define CONFIG_NETMASK 255.255.255.0
#define CONFIG_IPADDR 192.168.1.120
#define CONFIG_SERVERIP 192.168.1.101
#define CONFIG_ETHADDR 00:0c:29:d3:fe:1d
b. 设置环境变量,之前boot里面设置参数的时候,set之后是不能save的,因为没有设置保存地址,所以这里修改保存地址
原先的值:
#define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + 0x070000) /* */
#define CONFIG_ENV_IS_IN_FLASH
#define CONFIG_ENV_SIZE 0x10000
/* allow to overwrite serial and ethaddr */
#define CONFIG_ENV_OVERWRITE
修改后的代码
#if 0
#define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + 0x070000) /* */
#define CONFIG_ENV_IS_IN_FLASH
#define CONFIG_ENV_SIZE 0x10000
/* allow to overwrite serial and ethaddr */
#define CONFIG_ENV_OVERWRITE
#else
#define CONFIG_ENV_IS_IN_NAND
#define CONFIG_ENV_OFFSET 0x00040000 /* 环境变量保存在40000这个地址 */
#define CONFIG_ENV_SIZE 0x0002000 /* 保存区的长度 */
#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE /* 擦除的长度 */
#endif
二. 裁剪内核
进入smdk2440.h
,把不需要的功能的宏定义都注释掉或者去掉
a. USB用不着
/************************************************************
* USB support (currently only works with D-cache off)
************************************************************/
//#define CONFIG_USB_OHCI
//#define CONFIG_USB_KEYBOARD
//#define CONFIG_USB_STORAGE
//#define CONFIG_DOS_PARTITION
b. RTC时钟不需要
/************************************************************
* RTC
************************************************************/
//#define CONFIG_RTC_S3C24X0
c.BOOTP
/*
* BOOTP 看不懂,我就全去掉了
*/
//#define CONFIG_BOOTP_BOOTFILESIZE
//#define CONFIG_BOOTP_BOOTPATH
//#define CONFIG_BOOTP_GATEWAY
//#define CONFIG_BOOTP_HOSTNAME
d. 看不懂,去掉一些不眼熟的
/*
* Command line configuration.
*/
#include
#define CONFIG_CMD_BSP
#define CONFIG_CMD_CACHE
#define CONFIG_CMD_DATE
//#define CONFIG_CMD_DHCP /* 动态获得IP地址 */
#define CONFIG_CMD_ELF
#define CONFIG_CMD_NAND
#define CONFIG_CMD_PING
#define CONFIG_CMD_REGINFO
//#define CONFIG_CMD_USB
e.文件系统,只要能烧写映像文件就行,用不着文件系统
/*
* File system 文件系统
*/
//#define CONFIG_CMD_FAT
//#define CONFIG_CMD_EXT2
//#define CONFIG_CMD_UBI
//#define CONFIG_CMD_UBIFS
//#define CONFIG_CMD_MTDPARTS
//#define CONFIG_MTD_DEVICE
//#define CONFIG_MTD_PARTITIONS
//#define CONFIG_YAFFS2
//#define CONFIG_RBTREE
编译。果然出现了一堆错误
/work/system/u-boot-2012.04.01/common/cmd_date.c:60: undefined reference to `rtc_reset'
/work/system/u-boot-2012.04.01/common/cmd_date.c:63: undefined reference to `rtc_get'
/work/system/u-boot-2012.04.01/common/cmd_date.c:72: undefined reference to `rtc_set'
/work/system/u-boot-2012.04.01/common/cmd_date.c:81: undefined reference to `rtc_get'
drivers/mtd/ubi/libubi.o: In function `ubi_detach_mtd_dev':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/build.c:925: undefined reference to `put_mtd_device'
drivers/mtd/ubi/libubi.o: In function `open_mtd_device':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/build.c:957: undefined reference to `get_mtd_device_nm'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/build.c:959: undefined reference to `get_mtd_device'
drivers/mtd/ubi/libubi.o: In function `ubi_init':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/build.c:1023: undefined reference to `put_mtd_device'
drivers/mtd/ubi/libubi.o: In function `process_lvol':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/vtbl.c:381: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/vtbl.c:381: undefined reference to `rb_next'
drivers/mtd/ubi/libubi.o: In function `ubi_eba_init_scan':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/eba.c:1193: undefined reference to `rb_first'
drivers/mtd/ubi/libubi.o: In function `ubi_scan_move_to_list':
/work/system/u-boot-2012.04.01/include/../drivers/mtd/ubi/scan.h:146: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ubi_eba_init_scan':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/eba.c:1193: undefined reference to `rb_next'
drivers/mtd/ubi/libubi.o: In function `leb_write_unlock':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/eba.c:322: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `leb_read_unlock':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/eba.c:238: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ltree_add_entry':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/eba.c:191: undefined reference to `rb_insert_color'
drivers/mtd/ubi/libubi.o: In function `find_wl_entry':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:419: undefined reference to `rb_first'
drivers/mtd/ubi/libubi.o: In function `ensure_wear_leveling':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:999: undefined reference to `rb_first'
drivers/mtd/ubi/libubi.o: In function `prot_tree_del':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:574: undefined reference to `rb_erase'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:575: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `wl_tree_add':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:241: undefined reference to `rb_insert_color'
drivers/mtd/ubi/libubi.o: In function `ubi_wl_init_scan':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1515: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1516: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1516: undefined reference to `rb_next'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1515: undefined reference to `rb_next'
drivers/mtd/ubi/libubi.o: In function `check_protection_over':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:665: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:675: undefined reference to `rb_erase'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:676: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ubi_wl_scrub_peb':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1266: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ubi_wl_put_peb':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:1204: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `prot_tree_add':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:389: undefined reference to `rb_insert_color'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:403: undefined reference to `rb_insert_color'
drivers/mtd/ubi/libubi.o: In function `wear_leveling_worker':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:796: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:805: undefined reference to `rb_erase'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:811: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:814: undefined reference to `rb_erase'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:819: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ubi_wl_get_peb':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:498: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:500: undefined reference to `rb_last'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:518: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/wl.c:533: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `ubi_scan_rm_volume':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:634: undefined reference to `rb_erase'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:632: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:638: undefined reference to `rb_erase'
drivers/mtd/ubi/libubi.o: In function `add_volume':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:219: undefined reference to `rb_insert_color'
drivers/mtd/ubi/libubi.o: In function `ubi_scan_add_used':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:556: undefined reference to `rb_insert_color'
drivers/mtd/ubi/libubi.o: In function `ubi_scan':
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:958: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:959: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:959: undefined reference to `rb_next'
/work/system/u-boot-2012.04.01/drivers/mtd/ubi/scan.c:958: undefined reference to `rb_next'
fs/ext2/libext2fs.o: In function `ext2fs_set_blk_dev':
/work/system/u-boot-2012.04.01/fs/ext2/dev.c:44: undefined reference to `get_partition_info'
fs/fat/libfat.o: In function `file_fat_detectfs':
/work/system/u-boot-2012.04.01/fs/fat/fat.c:1162: undefined reference to `dev_print'
fs/fat/libfat.o: In function `fat_register_device':
/work/system/u-boot-2012.04.01/fs/fat/fat.c:79: undefined reference to `get_partition_info'
fs/ubifs/libubifs.o: In function `insert_old_idx':
/work/system/u-boot-2012.04.01/fs/ubifs/tnc.c:105: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `ubifs_add_bud':
/work/system/u-boot-2012.04.01/fs/ubifs/log.c:86: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `insert_dead_orphan':
/work/system/u-boot-2012.04.01/fs/ubifs/orphan.c:129: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `ubifs_recover_size':
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1171: undefined reference to `rb_first'
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1214: undefined reference to `rb_next'
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1220: undefined reference to `rb_next'
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1221: undefined reference to `rb_erase'
fs/ubifs/libubifs.o: In function `add_ino':
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1050: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `remove_ino':
/work/system/u-boot-2012.04.01/fs/ubifs/recovery.c:1088: undefined reference to `rb_erase'
fs/ubifs/libubifs.o: In function `insert_node':
/work/system/u-boot-2012.04.01/fs/ubifs/replay.c:373: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `insert_dent':
/work/system/u-boot-2012.04.01/fs/ubifs/replay.c:448: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `insert_ref_node':
/work/system/u-boot-2012.04.01/fs/ubifs/replay.c:689: undefined reference to `rb_insert_color'
fs/ubifs/libubifs.o: In function `apply_replay_tree':
/work/system/u-boot-2012.04.01/fs/ubifs/replay.c:306: undefined reference to `rb_next'
/work/system/u-boot-2012.04.01/fs/ubifs/replay.c:294: undefined reference to `rb_first'
make: *** [u-boot] Error 1
book@book-desktop:/work/system/u-boot-2012.04.01$
看第一个错误,rtc的cmd_date
有问题,把这个的宏也直接注释掉
//#define CONFIG_CMD_DATE
然后看下面的错误,好像都和ubi有关,但是ubi已经去掉了啊,可能没有make clean。
先make clean,然后再重新编译。
果然没有错误了。
看一下bin有多大
ls -l u-boot.bin
返回:
-rwxr-xr-x 1 book book 216844 2016-08-13 23:38 u-boot.bin
两百多k,原先是400多K,的确小了很多。
三. 划分分区
划分区不能随便画,要看一下使用的内核是打算怎么分的,不然两边数据读取的时候都对不上。
tftp下载uImage内核,bootm启动,这个在之前应该写过,内核会在内核中启动,重新上电就没有了,测试的时候很方便,启动内核以后在打出来的数据里查看分区,它是这样写的
0x00000000-0x00040000 : "bootloader"
0x00040000-0x00060000 : "params"
0x00060000-0x00260000 : "kernel"
0x00260000-0x10000000 : "root"
我们boot也照着这个样子分区好了。命令mtdpart是和分区有关的,查找boot里有没有名字是mtdpart的文件,找到cmd_mtdpart,找到里面的初始化函数mtdparts_init,里面太乱了,还是看看别人怎么用好了,搜索这个关键词,随便进入一个文件,看到别人这么写的
#define CONFIG_CMD_MTDPARTS
#define MTDIDS_DEFAULT "nor0=TQM8xxM-0" /* 表示哪一个设备 */
#define MTDPARTS_DEFAULT "mtdparts=TQM8xxM-0:512k(u-boot)," \
"128k(params)," \
"1920k(kernel)," \
"5632(rootfs)," \
"4m(data)" \
直接复制到2440.h,修改"nor0=TQM8xxM-0"
中的nor
为nand
,“TQM8xxM”
改为“jz2440
”(两个TQM8xxM
都要改为一致,我当时就是只改了一个,报错如下)
invalid mtd device 'TQM8xxM-0'
(u-boot)
的大小改为256k,params
改为128k,kernel
改为2m,rootfs
改为“-”,“-”表示剩下的空间,(data)直接删掉,注意rootfs后面的逗号一定要去掉,我之前没去掉,报错
no partitions allowed after a fill-up partition
后来查代码才发现,只要后面有逗号,它就会认为下面还有划分空间,这时候你又已经写了“-”表示这段空间到末尾,所以会报错。
所以整段代码改为
#define CONFIG_CMD_MTDPARTS
#define MTDIDS_DEFAULT "nand0=jz2440-0" /* 表示哪一个设备 */
#define MTDPARTS_DEFAULT "mtdparts=jz2440-0:256k(u-boot)," \
"128k(params)," \
"2m(kernel)," \
"-(rootfs)" \
最后还要到board.c的
for (;;) {
main_loop();
}
之前加上一句话
run_command("mtdparts default", 0);
这是命令mtdparts default
,就是说每次boot启动以后先分区,因为分区是我们程序软规定的,每次启动都要规定一下。
编译,没有错误,烧写。
四. 测试分区是否好使
a. 输入命令mtd,返回结果:
device nand0 , # parts = 4
#: name size offset mask_flags
0: u-boot 0x00040000 0x00000000 0
1: params 0x00020000 0x00040000 0
2: kernel 0x00200000 0x00060000 0
3: rootfs 0x0fda0000 0x00260000 0
已经分区成功了
b. 使用TFTP下载,把名为uImage的内核下载到30000000的地址
tftp 30000000 uImage
c. 擦除内核要使用的分区
nand erase.part kernel
d. 将内核写入kernel
nand write.part kernel
e. 重启,等待3秒结束后启动内核,
f. 启动成功