OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:10 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How can I read & write sectors from / to a USB flash drive?
PostPosted: Sun May 07, 2017 7:57 am 
Offline
User avatar

Joined: Sat May 06, 2017 11:35 am
Posts: 10
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:)


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Sun May 07, 2017 8:19 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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].


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Sun May 07, 2017 8:50 am 
Offline
Member
Member

Joined: Wed Oct 30, 2013 1:57 pm
Posts: 306
Location: Germany
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.

_________________
managarm


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Sun May 07, 2017 9:28 am 
Offline
User avatar

Joined: Sat May 06, 2017 11:35 am
Posts: 10
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:)


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Sun May 07, 2017 10:09 pm 
Offline
Member
Member

Joined: Mon Jul 25, 2016 6:54 pm
Posts: 223
Location: Adelaide, Australia
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.


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Mon May 08, 2017 11:14 am 
Offline
Member
Member
User avatar

Joined: Sun Feb 09, 2014 7:11 pm
Posts: 89
Location: Within a meter of a computer
StudlyCaps wrote:
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.

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 #Cardinal-OS on freenode!


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Thu May 11, 2017 11:30 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
EladAshkcenazi335 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.


Top
 Profile  
 
 Post subject: Re: How can I read & write sectors from / to a USB flash dri
PostPosted: Sun May 14, 2017 8:58 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 14, 2013 6:01 pm
Posts: 442
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: 8infy, Bing [Bot] and 87 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group