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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
elad
Posts: 8
Joined: Sat May 06, 2017 11:35 am
Libera.chat IRC: elad

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

Post 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.]
Restart any PC in the easy way around:

mov eax,cr0
xor al,1
mov cr0,eax
lidt [illegal_idtr]
jmp 0:$

; done:)
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

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

Post 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.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

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

Post 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.
elad
Posts: 8
Joined: Sat May 06, 2017 11:35 am
Libera.chat IRC: elad

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

Post 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.
Restart any PC in the easy way around:

mov eax,cr0
xor al,1
mov cr0,eax
lidt [illegal_idtr]
jmp 0:$

; done:)
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

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

Post 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.
User avatar
hgoel
Member
Member
Posts: 89
Joined: Sun Feb 09, 2014 7:11 pm
Libera.chat IRC: hgoel
Location: Within a meter of a computer

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

Post 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.
"If the truth is a cruel mistress, than a lie must be a nice girl"
Working on Cardinal
Find me at [url=irc://chat.freenode.net:6697/Cardinal-OS]#Cardinal-OS[/url] on freenode!
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

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

Post 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.
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

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

Post by Geri »

if you boot from pendrive/memory card, it will be emulated through int13 chs tuple
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
Post Reply