📄 main.c
字号:
/* * To send file through USB for s3c2410's bootloader, vivi * * Copyright (C) 2006 thisway.diy@163.com */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/select.h>#include <sys/time.h>#include <errno.h>#define DMA_BUF_SIZE 0x8000const char version[] = "USB transmitter for VIVI, v1.00.0";const char author[] = "thisway.diy@163.com"; void printusage(char *name){ printf("Usage: %s <dev> <file> [address], send file to USB device\n", name); printf(" %s -v, show the version\n", name); printf("examples:\n"); printf("1. %s /dev/usb/viviUSB0 vmlinus\n", name); printf("2. %s /dev/usb/viviUSB0 yaffs_image 0x30000000\n", name);}int main(int argc, char *argv[]){ int i; int first = 1; int fd_dev; int fd_file; struct stat stat_buf; unsigned int ret; unsigned int num_remain = 2; unsigned int num_read = 0; unsigned int num_write = 0; unsigned int num_read_total = 0; unsigned int num_write_total = 0; unsigned int num_has_write_total = 0; unsigned int download_addr = 0x30000000; unsigned short check_sum = 0; unsigned char *buf = 0; struct timeval time_start; struct timeval time_end; unsigned int time_us; if (argc == 2 && strcmp(argv[1], "-v") == 0) { printf("Version: %s\n", version); printf("Complie Time: %s, %s\n", __DATE__, __TIME__); printf("Author: %s\n", author); return 0; } if (argc < 3 || argc > 4) { printusage(argv[0]); return -1; } fd_dev = open(argv[1], O_RDWR); if (fd_dev < 0) { printf("open %s\n", argv[1]); exit(1); } fd_file = open(argv[2], O_RDONLY); if (fd_file < 0) { printf("open %s\n", argv[2]); exit(1); } if (fstat(fd_file, &stat_buf)) { printf("Can't get status of file %s\n", argv[2]); exit(1); } printf("Size of file: %d byte(s)\n", (unsigned int)stat_buf.st_size); /* FORMAT: <ADDR(DATA):4>+<SIZE(n+10):4>+<DATA:n>+<CS:2> */ num_write_total = stat_buf.st_size + 10; if (argc == 4) { if (strncmp("0x", argv[3], 2) == 0) { sscanf(argv[3], "%x", &download_addr); } else { sscanf(argv[3], "%d", &download_addr); } } printf("Address of vivi to receive data: 0x%x\n", download_addr); buf = malloc(DMA_BUF_SIZE); if (!buf) { printf("Can't alloc memory\n"); exit(1); } memcpy(buf, &download_addr, 4); memcpy(buf + 4, &num_write_total, 4); gettimeofday(&time_start, 0); while (num_read_total < stat_buf.st_size) { num_read = read(fd_file, buf + (first ? 8 : 0), DMA_BUF_SIZE - (first ? 8 : 0)); if (num_read) { num_read_total += num_read; for (i = 0; i < num_read; i++) { check_sum += (unsigned short)buf[i]; } } else { printf("Can't read file, num_read = %d, num_read_total = %d\n", num_read, num_read_total); return -1; } num_write = num_read + (first ? 8 : 0); if (num_read_total == stat_buf.st_size) { for (i = 0; i < 2; i++) { if (num_write < DMA_BUF_SIZE) { buf[num_write++] = (unsigned char)(check_sum >> (i*8)); num_remain--; } } } ret = write(fd_dev, buf, num_write); if (ret != num_write) { printf("Error, can't send to vivi: num_write = %d, ret = %d\n", num_write, ret); return -1; } num_has_write_total += num_write; gettimeofday(&time_end, 0); time_us = (time_end.tv_sec - time_start.tv_sec) * 1000000 +(time_end.tv_usec - time_start.tv_usec); printf("\rTx: %d/%d, Percentage: %.1f%%, Speed: %.1fKB/s, Time: %.1fS", num_has_write_total, num_write_total, ((float)num_has_write_total)/((float)num_write_total)*100., ((float)num_has_write_total/1024)/((float)time_us/1000000), time_us/1000000.); if (first) { first = 0; } } if (num_remain == 2) { buf[0] = (unsigned char)check_sum; buf[1] = (unsigned char)(check_sum >> 8); } else if (num_remain == 1) { buf[0] = (unsigned char)(check_sum >> 8); } if (num_remain) { num_write = num_remain; ret = write(fd_dev, buf, num_write); if (ret != num_write) { printf("Error, can't send to vivi: num_write = %d, ret = %d\n", num_write, ret); return -1; } num_has_write_total += num_write; } gettimeofday(&time_end, 0); time_us = (time_end.tv_sec - time_start.tv_sec) * 1000000 +(time_end.tv_usec - time_start.tv_usec); printf("\rTx: %d/%d, Percentage: %.1f%%, Speed: %.1fKB/s, Time: %.1fS\n", num_has_write_total, num_write_total, ((float)num_has_write_total)/((float)num_write_total)*100., ((float)num_has_write_total/1024)/((float)time_us/1000000), time_us/1000000.); close(fd_dev); close(fd_file); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -