Page 1 of 1

How can I read & write sectors from / to a USB flash drive?

Posted: Sun May 07, 2017 7:57 am
by elad
I have my os project in a usb drive so it can be portable but I need be able to read and write data from / to the usb . [to give examples in asm code only.]

Re: How can I read & write sectors from / to a USB flash dri

Posted: Sun May 07, 2017 8:19 am
by Korona
You need a USB host controller driver (EHCI or XHCI for USB 2/3; USB 1 is likely too slow to be used as a boot drive) and a driver for the USB bulk-only mass storage class. Writing a host controller driver is non-trivial. Read up the wiki articles about USB. If you don't have much experience writing drivers you probably want to start with something easier first like reading sectors via ATA PIO and then via ATA bus mastering DMA. There are articles on the wiki on both of these topics.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Sun May 07, 2017 8:50 am
by no92
As far as reading and writing USB drives is concerned, Korona is absolutely right. Also, writing it in Assembly is nothing but stupid, as it results in unmaintainable and non-portable code.

Also, I'm rolling back your changes to the Wish List. There are two reasons for this: for once, they are categorized wrongly. Also, there is information on the Wiki about both topics.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Sun May 07, 2017 9:28 am
by elad
Korona wrote:You need a USB host controller driver (EHCI or XHCI for USB 2/3; USB 1 is likely too slow to be used as a boot drive) and a driver for the USB bulk-only mass storage class. Writing a host controller driver is non-trivial. Read up the wiki articles about USB. If you don't have much experience writing drivers you probably want to start with something easier first like reading sectors via ATA PIO and then via ATA bus mastering DMA. There are articles on the wiki on both of these topics.
well ,I don't have alot of experience writing drivers at all so maybe I should first put my project in something like an ssd and use ATA PIO to read and write data . It much less portable but the good side is that it's much simpler and faster than reading and writing into a usb drive :D . Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Sun May 07, 2017 10:09 pm
by StudlyCaps
EladAshkcenazi335 wrote:Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.
Just to say, this is a great idea no matter whether you want to run on native hardware in the future or not.
It's much easier to debug on a controlled, well documented, environment than on real hardware.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Mon May 08, 2017 11:14 am
by hgoel
StudlyCaps wrote:
elad wrote:Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.
Just to say, this is a great idea no matter whether you want to run on native hardware in the future or not.
It's much easier to debug on a controlled, well documented, environment than on real hardware.
Also, just wanted to add that at the same time, it's important to at least occasionally test on real hardware, emulated devices often have subtle differences in how they behave, which can lead to drivers that only work on VMs. That situation can be a nightmare to debug.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Thu May 11, 2017 11:30 pm
by bzt
elad wrote:I have my os project in a usb drive so it can be portable but I need be able to read and write data from / to the usb . [to give examples in asm code only.]
You can temporarily switch back to real mode and call BIOS interrupts to do the job. It's not nice, I admit, but it's something you can do in asm (~100 bytes of code). You won't need to write USB drivers if you rely on BIOS compatibility layer (originally that's what BIOS was designed for, hence the name, Basic Input / Output System). And it will work with all disks, USB, ATA whatever.

Re: How can I read & write sectors from / to a USB flash dri

Posted: Sun May 14, 2017 8:58 am
by Geri
if you boot from pendrive/memory card, it will be emulated through int13 chs tuple