How can I read & write sectors from / to a USB flash drive?
How can I read & write sectors from / to a USB flash drive?
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:)
mov eax,cr0
xor al,1
mov cr0,eax
lidt [illegal_idtr]
jmp 0:$
; done:)
Re: How can I read & write sectors from / to a USB flash dri
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].
-
- 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
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.
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
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 . Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.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.
Restart any PC in the easy way around:
mov eax,cr0
xor al,1
mov cr0,eax
lidt [illegal_idtr]
jmp 0:$
; done:)
mov eax,cr0
xor al,1
mov cr0,eax
lidt [illegal_idtr]
jmp 0:$
; done:)
-
- 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
Just to say, this is a great idea no matter whether you want to run on native hardware in the future or not.EladAshkcenazi335 wrote:Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.
It's much easier to debug on a controlled, well documented, environment than on real hardware.
- hgoel
- 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
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.StudlyCaps wrote:Just to say, this is a great idea no matter whether you want to run on native hardware in the future or not.elad wrote:Or I can use virtual machine and something like that to emulate a disk drive instead of a usb drive.
It's much easier to debug on a controlled, well documented, environment than on real hardware.
"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!
Working on Cardinal
Find me at [url=irc://chat.freenode.net:6697/Cardinal-OS]#Cardinal-OS[/url] on freenode!
Re: How can I read & write sectors from / to a USB flash dri
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.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.]
Re: How can I read & write sectors from / to a USB flash dri
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
http://users.atw.hu/gerigeri/DawnOS/download.html