943,923 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3613
  • C RSS
May 28th, 2006
0

Reading from hard disk

Expand Post »
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?
Similar Threads
Reputation Points: 17
Solved Threads: 5
Posting Whiz in Training
mmiikkee12 is offline Offline
274 posts
since Oct 2004
May 28th, 2006
0

Re: Reading from hard disk

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);
Reputation Points: 17
Solved Threads: 5
Posting Whiz in Training
mmiikkee12 is offline Offline
274 posts
since Oct 2004
May 28th, 2006
0

Re: Reading from hard disk

http://c-faq.com/struct/padding.html
How you specify a packed structure depends on your compiler.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Macro Listing
Next Thread in C Forum Timeline: _posix_thread_process_shared





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC