第0章-可以在smart210上应用的dnw for linux源码----这只是个开始

来源:互联网 发布:什么是聚类算法 编辑:程序博客网 时间:2024/05/04 00:57

        前些天(大概是2015年的2月下旬吧,反正是过完年回来)购买了一款smart210的开发板,后来发现这个配套的superboot210.bin其实并不是很人性化,它很强大,但是不开源。我们甚至都不知道它为boot.bin,parameters还有kernel分了多大的分区。无奈之下(越无奈,越是进步的开始)自己从网上找到了一个能跑起来的smart210的uboot,非常开心它能跑起来,但是可惜的是,它并不能正确的引导内核,于是一场关于嵌入式Linux的精彩游戏便开始了。

        我的目标是用我手头上的这个uboot源代码,实现文件下载,并正确引导内核,然后争取基本替代superboot210.bin。说基本替代,是因为我只想用superboot210.bin帮我从SD卡启动,然后下载一个自己的uboot.bin到nand flash中,然后用自己的uboot向nand flash里下载内核和yaffs2格式的根文件系统。当然如果可能,我也盼望着能把自己的uboot.bin烧到SD卡里,能够从SD卡正常启动。那会就真正的抛开superboot210.bin,向着这个目标前进吧。

      下面的输出信息,是我的uboot的一开始的样子

########################################################
#   Modified by learn_zhang #
########################################################


U-Boot 2011.06 (Mar 16 2015 - 18:38:24) for Smart210

CPU:    S5PC110@1000MHz

Board:   Smart210
DRAM:  512 MiB
WARNING: Caches not enabled

PWM Moudle Initialized.
NAND:  512 MiB
MMC:   SAMSUNG SD/MMC: 0, SAMSUNG SD/MMC: 1
In:    serial
Out:   serial
Err:   serial
Net:   dm9000

 
Hit any key to stop autoboot:  0

#####     Boot for Smart210 Main Menu    #####
#####    Smart210 USB download mode     #####

[1] Download program to Nand Flash
[2] Download Linux Kernel (uImage.bin) to Nand Flash
[3] Download YAFFS image (root.bin) to Nand Flash
[4] Download Program to SDRAM and Run it
[5] Boot the system
[6] Format the Nand Flash
[q] Return main Menu
Enter your selection:

      可以看到,uboot能够支持dnw下载,所以首先要让找个一个合适的dnw for linux源码。这个网上已经有现成的了。但是网上大部分的例程中使用的是2440/2410/6440开发板,直接使用网上的dnw源码烧写s5pv210开发板会遇到错误找不到设备或者是usb_bulk_write这个函数执行不正确对于找不到设备这种情况,需要在开发板连上电脑之后运行lsusb功能,找到开发板对应的序列号,然后修改对应的源码然后重新编译运行即可而如果是usb_bulk_write函数执行错误,则找到源码中usb_bulk_write这个函数,将第二个参数endpoint从0x03改成0x02然后重新编译运行即可(参考 http://www.xuebuyuan.com/1908524.html)

   下面是我使用的源码,感谢前辈的贡献。注意先安装libusb-dev,然后编译的时候输入gcc dnw.c -o dnw -lusb

/* dnw2 linux main file. This depends on libusb.
*
* Author:         Fox <hulifox008@163.com>
* License:        GPL
* sudo apt-get install libusb-dev
* gcc dnw.c -o dnw -lusb
*/



#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define                 ZBL210_SECBULK_IDVENDOR                0x04e8
#define                ZBL210_SECBULK_IDPRODUCT        0x1234


struct usb_dev_handle * open_port()
{
        struct usb_bus *busses, *bus;

        usb_init();
        usb_find_busses();
        usb_find_devices();

        busses = usb_get_busses();
        for(bus=busses;bus;bus=bus->next)
        {
                 struct usb_device *dev;
                for(dev=bus->devices;dev;dev=dev->next)
                {
                printf("idVendor:0x%x\t,ipProduct:0x%x\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
                        if( ZBL210_SECBULK_IDVENDOR==dev->descriptor.idVendor
                        &&  ZBL210_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
                        {
                                printf("Target usb device found!\n");
                                struct usb_dev_handle *hdev = usb_open(dev);
                                if(!hdev)
                                {
                                        perror("Cannot open device");        
                                }
                                else
                                {
                                        if(0!=usb_claim_interface(hdev, 0))
                                        {
                                                perror("Cannot claim interface");
                                                usb_close(hdev);
                                                hdev = NULL;
                                        }
                                }
                                return hdev;
                        }
                }
        }
        
        printf("Target usb device not found!\n");

        return NULL;
}

void usage()
{
        printf("Usage: dnw2 <file>\n\n");
}

unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
        unsigned char *write_buf = NULL;
        struct stat fs;

        int fd = open(filename, O_RDONLY);
        if(-1==fd)
        {
                perror("Cannot open file");
                return NULL;
        }
        if(-1==fstat(fd, &fs))
        {
                perror("Cannot get file size");
                goto error;
        }
        write_buf = (unsigned char*)malloc(fs.st_size+10);
        if(NULL==write_buf)
        {
                perror("malloc failed");
                goto error;
        }

        if(fs.st_size != read(fd, write_buf+8, fs.st_size))
        {
                perror("Reading file failed");
                goto error;
        }

        printf("Filename : %s\n", filename);
        printf("Filesize : %d bytes\n", (int)fs.st_size);

        *((u_int32_t*)write_buf) = 0x30000000;                //download address
        *((u_int32_t*)write_buf+1) = fs.st_size + 10;        //download size;

        *len = fs.st_size + 10;
        return write_buf;

error:
        if(fd!=-1) close(fd);
        if(NULL!=write_buf) free(write_buf);
        fs.st_size = 0;
        return NULL;
        
}

int main(int argc, char *argv[])
{
        if(2!=argc)
        {
                usage();
                return 1;
        }

        struct usb_dev_handle *hdev = open_port();
        if(!hdev)
        {
                return 1;
        }

        unsigned int len = 0;
        unsigned char* write_buf = prepare_write_buf(argv[1], &len);
        if(NULL==write_buf) return 1;

        unsigned int remain = len;
        unsigned int towrite;
        printf("Writing data ...\n");
        while(remain)
        {
                towrite = remain>512 ? 512 : remain;
                if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 3000))
                {
                        perror("usb_bulk_write failed");
                        break;
                }
                remain-=towrite;
                printf("\r%d%\t %d bytes     ", (len-remain)*100/len, len-remain);
                fflush(stdout);
        }
        if(0==remain) printf("Done!\n");
        return 0;
}

 



0 0