mkbl2:Usage: unsupported size

在前面的文章 tiny4412 led 裸机程序中遇到一个问题,使用 mkbl2 来将裸机程序制作成 BL2.bin 时报错。
  ./mkbl2 led.bin bl2.bin 14336 
  Usage: unsupported size
  我尝试把 14336 改成 led.bin 文件的实际大小后,烧写到 emmc 程序无法按照预期执行。那么问题出在哪?
  

  BL2 位于设备偏移地址(512 +8K)字节处,BL1从这个位置读入 14K 字节的数据,存在iRAM 地址 0x02023400 处。 BL2不能大于(14K – 4)字节,最后 4字节用于存放较验码。

  那么我猜测,当我们的 led.bin 小于 14K 的时候,我们生成的 bl2.bin 也得是14K大小,并且最后4字节是校验码。

来看看 mkbl2 的源码:

[cpp]  view plain  copy
  1. /* 
  2.  * Copyright (c) 2010 Samsung Electronics Co., Ltd. 
  3.  *              http://www.samsung.com/ 
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify 
  6.  * it under the terms of the GNU General Public License version 2 as 
  7.  * published by the Free Software Foundation. 
  8.  */  
  9.   
  10. #include <stdio.h>  
  11. #include <string.h>  
  12. #include <stdlib.h>  
  13.   
  14. int main (int argc, char *argv[])  
  15. {  
  16.     FILE        *fp;  
  17.     unsigned char   src;  
  18.     char        *Buf, *a;  
  19.     int     BufLen;  
  20.     int     nbytes, fileLen;  
  21.     unsigned int    checksum = 0;  
  22.     int     i;  
  23.   
  24.     if (argc != 4)  
  25.     {  
  26.         printf("Usage: mkbl1 <source file> <destination file> <size> \n");  
  27.         return -1;  
  28.     }  
  29.   
  30.     BufLen = atoi(argv[3]);  
  31.     Buf = (char *)malloc(BufLen);  
  32.     memset(Buf, 0x00, BufLen);  
  33.   
  34.     fp = fopen(argv[1], "rb");  
  35.     if( fp == NULL)  
  36.     {  
  37.         printf("source file open error\n");  
  38.         free(Buf);  
  39.         return -1;  
  40.     }  
  41.   
  42.     fseek(fp, 0L, SEEK_END);  
  43.     fileLen = ftell(fp);  
  44.     fseek(fp, 0L, SEEK_SET);  
  45.   
  46.     if ( BufLen > fileLen )  
  47.     {  
  48.         printf("Usage: unsupported size\n");  
  49.         free(Buf);  
  50.         fclose(fp);  
  51.         return -1;  
  52.     }  
  53.   
  54.     nbytes = fread(Buf, 1, BufLen, fp);  
  55.   
  56.     if ( nbytes != BufLen )  
  57.     {  
  58.         printf("source file read error\n");  
  59.         free(Buf);  
  60.         fclose(fp);  
  61.         return -1;  
  62.     }  
  63.   
  64.     fclose(fp);  
  65.   
  66.     for(i = 0;i < (14 * 1024) - 4;i++)  
  67.     {  
  68.         checksum += (unsigned char)(Buf[i]);  
  69.     }  
  70.     *(unsigned int*)(Buf+i) = checksum;  
  71.   
  72.     fp = fopen(argv[2], "wb");  
  73.     if (fp == NULL)  
  74.     {  
  75.         printf("destination file open error\n");  
  76.         free(Buf);  
  77.         return -1;  
  78.     }  
  79.   
  80.     a   = Buf;  
  81.     nbytes  = fwrite( a, 1, BufLen, fp);  
  82.   
  83.     if ( nbytes != BufLen )  
  84.     {  
  85.         printf("destination file write error\n");  
  86.         free(Buf);  
  87.         fclose(fp);  
  88.         return -1;  
  89.     }  
  90.   
  91.     free(Buf);  
  92.     fclose(fp);  
  93.   
  94.     return 0;  
  95. }  
  代码很简单,问题很明显,当我们的 Led.bin 小于14K时,我们输入文件实际大小,Bufsize == filesize ,但是它在计算校验的时候居然是按照 14k 的大小来计算的,数组都溢出了!!!

修改:

[cpp]  view plain  copy
  1. /* 
  2.  * Copyright (c) 2010 Samsung Electronics Co., Ltd. 
  3.  *              http://www.samsung.com/ 
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify 
  6.  * it under the terms of the GNU General Public License version 2 as 
  7.  * published by the Free Software Foundation. 
  8.  */  
  9.   
  10. #include <stdio.h>  
  11. #include <string.h>  
  12. #include <stdlib.h>  
  13.   
  14. int main (int argc, char *argv[])  
  15. {  
  16.     FILE        *fp;  
  17.     unsigned char   src;  
  18.     char        *Buf, *a;  
  19.     int     BufLen;  
  20.     int     nbytes, fileLen;  
  21.     unsigned int    checksum = 0;  
  22.     int     i;  
  23.   
  24.     if (argc != 4)  
  25.     {  
  26.         printf("Usage: mkbl1 <source file> <destination file> <size> \n");  
  27.         return -1;  
  28.     }  
  29.   
  30.     BufLen = atoi(argv[3]);  
  31.     Buf = (char *)malloc(BufLen);  
  32.     memset(Buf, 0x00, BufLen);  
  33.   
  34.     fp = fopen(argv[1], "rb");  
  35.     if( fp == NULL)  
  36.     {  
  37.         printf("source file open error\n");  
  38.         free(Buf);  
  39.         return -1;  
  40.     }  
  41.   
  42.     fseek(fp, 0L, SEEK_END);  
  43.     fileLen = ftell(fp);  
  44.     fseek(fp, 0L, SEEK_SET);  
  45. /* 
  46.     if ( BufLen > fileLen ) 
  47.     { 
  48.         printf("Usage: unsupported size\n"); 
  49.         free(Buf); 
  50.         fclose(fp); 
  51.         return -1; 
  52.     } 
  53. */  
  54.     //nbytes = fread(Buf, 1, BufLen, fp);  
  55.     if(BufLen > fileLen)  
  56.         nbytes = fread(Buf, 1, fileLen, fp);  
  57.     else  
  58.         nbytes = fread(Buf, 1, BufLen, fp);  
  59. /* 
  60.     if ( nbytes != BufLen ) 
  61.     { 
  62.         printf("source file read error\n"); 
  63.         free(Buf); 
  64.         fclose(fp); 
  65.         return -1; 
  66.     } 
  67. */  
  68.     fclose(fp);  
  69.   
  70.     for(i = 0;i < (14 * 1024) - 4;i++)  
  71.     {  
  72.         checksum += (unsigned char)(Buf[i]);  
  73.     }  
  74.     *(unsigned int*)(Buf+i) = checksum;  
  75.   
  76.     fp = fopen(argv[2], "wb");  
  77.     if (fp == NULL)  
  78.     {  
  79.         printf("destination file open error\n");  
  80.         free(Buf);  
  81.         return -1;  
  82.     }  
  83.   
  84.     a   = Buf;  
  85.     nbytes  = fwrite( a, 1, BufLen, fp);  
  86.   
  87.     if ( nbytes != BufLen )  
  88.     {  
  89.         printf("destination file write error\n");  
  90.         free(Buf);  
  91.         fclose(fp);  
  92.         return -1;  
  93.     }  
  94.   
  95.     free(Buf);  
  96.     fclose(fp);  
  97.   
  98.     return 0;  
  99. }  
  

  1. gcc -o mkbl2 V310-EVT1-mkbl2.c  
  以后我们在使用 mkbl2 的时候无论文件大小是多少,都写死为 14336 就好了,例如:

  mkbl2 led.bin bl2.bin 14336

  实验验证上述做法是正确的,对4412的启动流程的了解又加深了一步。

总结:

  mkbl2 原理:读取源文件14k-4字节,计算校验和,写入14k-4至14k的地方,生成bl2.bin。

  无论是sd卡,还是emmc,BL2存放在第17个扇区,大小14K,校验和必须存放在14k-4的位置,估计BL1会到这个位置取出校验和进行校验文件是否损坏。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值