2010年1月28日 星期四

How to write Linux version DNW (a Samsung USB fusing tool)

1. 參考http://chengchungchun.spaces.live.com/blog/cns!EEDBBE1B9C2FFA9D!1484.entry

2. "dnw_linux" 是由二個程式所組成:dnw.c 和 secbulk.c。dnw.c負責製作dnw packet。secbulk.c負責將dnw packet透過usb end point將資料送到target端。

3. cd dnw_linux/dnw;gcc -o dnw dnw.c

4. cd dnw_linux/secbulk;make -C /lib/modules/`uname -r`/build M=`pwd` modules

6. 執行方式

(1) Use Putty to connect the board. Under Uboot keyin the following command:

dnw 32000000

(2) Under the host computer, keyin the following command:
cd dnw_linux/secbulk
sudo insmod ./secbulk.ko
cd dnw_linux/dnw
sudo sudo ./dnw u-boot.bin

7. dnw_linux/dnw/dnw.c:

#include stdio.h
#include stdlib.h
#include malloc.h
#include sys/types.h
#include sys/stat.h
#include unistd.h
#include fcntl.h

const char* dev = "/dev/secbulk0";


int main(int argc, char* argv[])
{
unsigned char* file_buffer = NULL;
if( 2 != argc )
{
printf("Usage: dwn \n");
return 1;
}

int fd = open(argv[1], O_RDONLY);
if(-1 == fd)
{
printf("Can not open file - %s\n", argv[1]);
return 1;
}

struct stat file_stat;
if( -1 == fstat(fd, &file_stat) )
{
printf("Get file size filed!\n");
return 1;
}

file_buffer = (char*)malloc(file_stat.st_size+10);
if(NULL == file_buffer)
{
printf("malloc failed!\n");
goto error;
}
if( file_stat.st_size != read(fd, file_buffer+8, file_stat.st_size))
{
printf("Read file failed!\n");
goto error;
}

printf("file name : %s\n", argv[1]);
printf("file size : %d bytes\n", file_stat.st_size);

int fd_dev = open(dev, O_WRONLY);
if( -1 == fd_dev)
{
printf("Can not open %s\n", dev);
goto error;
}

*((unsigned long*)file_buffer) = 0x32000000; //load address
*((unsigned long*)file_buffer+1) = file_stat.st_size+10; //file size
unsigned short sum = 0;
int i;
for(i=8; i {
sum += file_buffer[i];
}
*((unsigned short*)(file_buffer + file_stat.st_size + 8)) = sum;
printf("Writing data...\n");
size_t remain_size = file_stat.st_size+10;
size_t block_size = 512;//remain_size / 100;
size_t writed = 0;
while(remain_size>0)
{
size_t to_write = remain_size > block_size ? block_size:remain_size;
if( to_write != write(fd_dev, file_buffer+writed, to_write))
{
printf("failed!\n");
return 1;
}
remain_size -= to_write;
writed += to_write;
printf("\r%d%\t %d bytes ", writed*100/(file_stat.st_size+10), writed);
fflush(stdout);

}

printf("OK\n");
return 0;

error:
if(-1!=fd_dev) close(fd_dev);
if(fd != -1) close(fd);
if( NULL!=file_buffer )
free(file_buffer);
return -1;
}
8. dnw_linux/secbulk/secbulk.c:

#include linux/kernel.h
#include linux/module.h
#include linux/usb.h
#include linux/fs.h
#include linux/mutex.h
#include linux/slab.h
#include linux/uaccess.h

#define SECBULK_MAJOR 102
#define SECBULK_MINOR 0
#define DRIVER_NAME "secbulk"

#define BULKOUT_BUFFER_SIZE 512

struct secbulk_dev
{
struct usb_device *udev;
struct mutex io_mutex;
char* bulkout_buffer;
__u8 bulk_out_endpointAddr;
};

static struct usb_class_driver secbulk_class;
//idVendor=04e8, idProduct=1234
static struct usb_device_id secbulk_table[]= {
// { USB_DEVICE(0x5345, 0x1234)},
{ USB_DEVICE(0x04e8, 0x1234)},
{ }
};

static struct usb_driver secbulk_driver;
static void secbulk_disconnect(struct usb_interface *interface)
{
struct secbulk_dev *dev = NULL;
printk(KERN_INFO "secbulk:secbulk disconnected!\n");
dev = usb_get_intfdata(interface);
if( NULL != dev )
kfree(dev);
usb_deregister_dev(interface, &secbulk_class);
return;
}

static ssize_t secbulk_read(struct file *file, char __user *buf, size_t len, loff_t *loff)
{
return -EPERM;
}

static ssize_t secbulk_write(struct file *file, const char __user *buf, size_t len, loff_t *loff)
{
size_t to_write;
struct secbulk_dev *dev = file->private_data;
int ret;
int actual_length;
size_t total_writed;

total_writed = 0;
while(len > 0)
{
to_write = min(len, BULKOUT_BUFFER_SIZE);

if(copy_from_user(dev->bulkout_buffer, buf+total_writed, to_write))
{
printk(KERN_ERR "secbulk:copy_from_user failed!\n");
return -EFAULT;
}

ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
dev->bulkout_buffer,
to_write,
&actual_length,
3*HZ);
if(ret || actual_length!=to_write)
{
printk(KERN_ERR "secbulk:usb_bulk_msg failed!\n");
return -EFAULT;
}
len -= to_write;
total_writed += to_write;
}
return total_writed;
}

static int secbulk_open(struct inode *node, struct file *file)
{
struct usb_interface *interface;
struct secbulk_dev *dev;

interface = usb_find_interface(&secbulk_driver, iminor(node));
if(!interface)
return -ENODEV;

dev = usb_get_intfdata(interface);
dev->bulkout_buffer = kzalloc(BULKOUT_BUFFER_SIZE, GFP_KERNEL);
if(!(dev->bulkout_buffer))
return -ENOMEM;
if(!mutex_trylock(&dev->io_mutex))
return -EBUSY;
file->private_data = dev;
return 0;
}

static int secbulk_release(struct inode *node, struct file *file)
{
struct secbulk_dev *dev;

dev = (struct secbulk_dev*)(file->private_data);
kfree(dev->bulkout_buffer);
mutex_unlock(&dev->io_mutex);
return 0;
}

static struct file_operations secbulk_fops = {
.owner = THIS_MODULE,
.read = secbulk_read,
.write = secbulk_write,
.open = secbulk_open,
.release= secbulk_release,
};

static struct usb_class_driver secbulk_class = {
.name = "secbulk%d",
.fops = &secbulk_fops,
.minor_base= 100,
};

static int secbulk_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
int ret;
struct secbulk_dev *dev;
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint;
int i;

printk(KERN_INFO "secbulk:secbulk probing...\n");

dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if(!dev)
{
ret = -ENOMEM;
goto error;
}

iface_desc = interface->cur_altsetting;
for(i=0; i < iface_desc->desc.bNumEndpoints; i++)
{
endpoint = &(iface_desc->endpoint[i].desc);
if(!dev->bulk_out_endpointAddr
&& usb_endpoint_is_bulk_out(endpoint))
{
printk(KERN_INFO "secbulk:bulk out endpoint found!\n");
dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
break;
}
}

if(!(dev->bulk_out_endpointAddr))
{
ret = -EBUSY;
goto error;
}

ret = usb_register_dev(interface, &secbulk_class);
if(ret)
{
printk(KERN_ERR "secbulk: usb_register_dev failed!\n");
return ret;
}
dev->udev = usb_get_dev(interface_to_usbdev(interface));

usb_set_intfdata(interface, dev);

mutex_init(&dev->io_mutex);
return 0;
error:
if(!dev)
kfree(dev);
return ret;
}

static struct usb_driver secbulk_driver= {
.name= "secbulk",
.probe= secbulk_probe,
.disconnect= secbulk_disconnect,
.id_table= secbulk_table,
.supports_autosuspend=0,
};
static int __init secbulk_init(void)
{
int result;
printk(KERN_INFO "secbulk:secbulk loaded\n");
result = usb_register(&secbulk_driver);
if(result)
{ printk(KERN_ERR "secbulk:usb_register failed: %d", result);
return result;
}
return 0;
}

static void __exit secbulk_exit(void)
{
usb_deregister(&secbulk_driver);
printk(KERN_INFO "secbulk:secbulk unloaded\n");
}

module_init(secbulk_init);
module_exit(secbulk_exit);
MODULE_LICENSE("GPL");

9.dnw_linux/secbulk/Makefile:

obj-m := secbulk.o
secbulk-m := secbulk.o

35 則留言:

匿名 提到...

I would like to exchange links with your site www.blogger.com
Is this possible?

匿名 提到...

where can i buy xanax online legally xanax dosage by body weight - buy cheap alprazolam no prescription

匿名 提到...

buy xanax 2mg generic xanax works best - robaxin xanax drug interactions

匿名 提到...

buy tramadol online tramadol withdrawal wiki - buy tramadol thailand

匿名 提到...

buy tramadol online can take 3 50 mg tramadol - buy tramadol online no prescription cheap

匿名 提到...

tramadol online no prescription tramadol withdrawal night sweats - tramadol dosage in adults

匿名 提到...

buy tramadol online naltrexone for tramadol addiction - buy tramadol 50mg online

匿名 提到...

buy tramadol online tramadol generic - can you buy tramadol online legally

匿名 提到...

xanax online xanax 2mg - xanax on drug test time

匿名 提到...

buy tramadol online tramadol for dogs side effects panting - tramadol online american express

匿名 提到...

generic xanax xanax withdrawal youtube - xanax dosage by color

匿名 提到...

buy tramadol online why can you buy tramadol online - tramadol 50 mg for migraine

匿名 提到...

generic xanax pills similar to xanax - xanax high school

匿名 提到...

xanax online drug test results for xanax - xanax erowid

匿名 提到...

buy tramadol no prescription tramadol hcl ultram 50 mg - tramadol 100 mg side effects

匿名 提到...

xanax online order xanax by mail - buy alprazolam 2mg online

匿名 提到...

xanax online xanax on drug urine test - generic xanax s 901

匿名 提到...

xanax online buy xanax online yahoo answers - xanax bars g3722 effects

匿名 提到...

xanax online small round white pill xanax - xanax withdrawal vision

匿名 提到...

cialis online generic cialis names - cialis 5 mg daily dosage

匿名 提到...

tramadol online overnight tramadol addiction withdrawal - tramadol gea 100mg

匿名 提到...

07 cheap zyban - generic zyban http://www.wellbutrinforsaleonline.net/, [url=http://www.wellbutrinforsaleonline.net/]zyban online [/url]

匿名 提到...

1, lasix cost - lasix online http://www.lasixordernow.net/#lasix-online, [url=http://www.lasixordernow.net/#lasix-online]cheap furosemide[/url]

匿名 提到...

1, buy meridia - buy sibutramine no prescription http://www.meridiaforyourdiet.net/#sibutramine-diet-pills, generic meridia

匿名 提到...

legal buy tramadol online buy tramadol online yahoo - order tramadol online cod

匿名 提到...

buy tramadol tramadol uss ship no prescription - where to buy tramadol online

匿名 提到...

http://reidmoody.com/#54126 ativan or xanax which is better - ativan sublingual uses

匿名 提到...

http://ranchodelastortugas.com/#50238 xanax drug insert - xanax to get high

匿名 提到...

buy tramadol with paypal tramadol withdrawal blog - tramadol high length

匿名 提到...

http://ranchodelastortugas.com/#82165 ritalin xanax drug interactions - xanax tablets 0.25 mg

匿名 提到...

http://ranchodelastortugas.com/#51462 generic for xanax pill pictures - xanax dosage yahoo answers

匿名 提到...

Υour current reροrt ρrovіԁes рroven useful to
myѕelf. It’s quite useful and you're simply naturally extremely well-informed in this region. You possess popped my own face in order to varying thoughts about this particular subject along with intriquing, notable and strong articles.
Here is my blog post ... soma

匿名 提到...

Hello! I just wish to give an enormous thumbs up for the nice info you have got here on this post.
I can be coming again to your weblog for extra soon.



Here is my homepage :: seo e google adwords

匿名 提到...

Hi there! I simply would like to give a huge thumbs up for the nice data you may
have right here on this post. I shall be coming back to your weblog for more soon.


My webpage ... seoul subway map smrt

匿名 提到...

Hello! I simply want to give a huge thumbs up for the good data you’ve gotten right here on this post.
I shall be coming back to your blog for extra soon.


My blog post we got married seohyun yonghwa facebook