Reading from hard disk

Reply

Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Reading from hard disk

 
0
  #1
May 28th, 2006
First let me point out that this is not homework, it's just something I'm doing because I'm bored ;-)
I'm trying to get my little hobby operating system to read from the hard disk. So far all I can get is the byte FF (255). I've tested this in both Bochs and QEMU and the same thing happens. Here's the hard disk reading part of my code:
  1. #include <harddisk.h>
  2. #include <types.h>
  3.  
  4. #define CMD_READ 0x20
  5. #define CMD_WRITE 0x30
  6. // NR = no retry
  7. #define CMD_READ_NR 0x21
  8. #define CMD_WRITE_NR 0x31
  9.  
  10. byte *read_sector(uint_8 drivenum, uint_8 sector, uint_16 cylinder, uint_8 *buffer)
  11. {
  12. uint_8 drive;
  13. uint_16 port_base;
  14. // Yes, I know I could use some binary operations.
  15. // This is a lot easier to read though.
  16. switch (drivenum)
  17. {
  18. case 0:
  19. drive = 0xA0;
  20. port_base = (uint_8)0x1f0;
  21. break;
  22. case 1:
  23. drive = 0xB0;
  24. port_base = (uint_8)0x1f0;
  25. break;
  26. case 2:
  27. drive = 0xA0;
  28. port_base = (uint_8)0x170;
  29. break;
  30. case 3:
  31. drive = 0xB0;
  32. port_base = (uint_8)0x170;
  33. // If I ever get bored enough to add them, 0x1e8 for
  34. // ide2 and 0x168 for ide3.
  35. }
  36.  
  37. // send some info to the IDE controller
  38. // drive to read from
  39. outportb(port_base + 6, drive);
  40. // just one sector
  41. outportb(port_base + 2, 1);
  42. // sector
  43. outportb(port_base + 3, sector);
  44. // extract low and high bytes from cylinder number
  45. uint_8 cyl_low = cylinder & 0xFF;
  46. uint_8 cyl_high = (cylinder & 0xFF00) >> 8;
  47. // ...then send them to the IDE controller
  48. outportb(port_base + 4, cyl_low);
  49. outportb(port_base + 5, cyl_high);
  50. // and now send the command
  51. outportb(port_base + 7, CMD_READ);
  52.  
  53.  
  54.  
  55. // now read the data in from the buffer
  56. int i;
  57. for (i = 0; i < 256; i++)
  58. {
  59. buffer[i] = inportw(port_base);
  60. // debugging
  61. printf("%x\t| %d\t| %c\n", buffer[i], buffer[i], buffer[i]);
  62. }
  63. return buffer;
  64. }
I'm calling it like this:
  1. uint_8 *data = (uint_8 *)0x300000;
  2. read_sector(0, 1, 1, data);
The only thing the debug printf will print is "ff | 255 | ". Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: Reading from hard disk

 
0
  #2
May 28th, 2006
OK, just got that working. Turns out I was reading into the buffer wrong. Now I have another problem. This *should* be 512 bytes, I added it up myself. But sizeof() gives me 516, and I get some random, obviously incorrect values when I print out the partition info. (A byte with only the top bit set is never 192.) Any ideas?
  1. typedef struct
  2. {
  3. uint_8 active;
  4. uint_8 start_head;
  5. uint_8 start_sector;
  6. uint_8 start_cylinder;
  7. uint_8 partition_type;
  8. uint_8 end_head;
  9. uint_8 end_sector;
  10. uint_8 end_cylinder;
  11. uint_32 start_lba;
  12. uint_32 end_lba;
  13. } partition;
  14.  
  15. typedef struct
  16. {
  17. uint_8 useless_data[446];
  18. partition partitions[4];
  19. uint_16 signature;
  20. } partition_table;
  21.  
  22. void *read_sector(uint_8 drivenum, uint_16 cylinder, uint_8 sector, uint_8 head, uint_8 *buffer);
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading from hard disk

 
0
  #3
May 28th, 2006
http://c-faq.com/struct/padding.html
How you specify a packed structure depends on your compiler.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC