Partition Table Woes :'(

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
SystemHalted

Partition Table Woes :'(

Post by SystemHalted »

Hmm.. When I read from the partition tables the data is weird
Normal Layout: 80C.H.S.06C.H.S.LBASect.LBASize.
What I get: 0180??????06????LBASect.LBASize.
I get this on all my computers, I'm temted to push on amd use the lba values to make chs values and use the values that i know.
heres my code:
typedef struct{
byte Bsect[446];
byte Active1;
byte Sect1;
word HeadCyl1;
byte Part1;
byte ESect1;
word EHeadCyl1;
dword SLBA1;
dword Size1;
byte Active2;
byte Sect2;
word HeadCyl2;
byte Part2;
byte ESect2;
word EHeadCyl2;
dword SLBA2;
dword Size2;
byte Active3;
byte Sect3;
word HeadCyl3;
byte Part3;
byte ESect3;
word EHeadCyl3;
dword SLBA3;
dword Size3;
byte Active4;
byte Sect4;
word HeadCyl4;
byte Part4;
byte ESect4;
word EHeadCyl4;
dword SLBA4;
dword Size4;
word Sig;
}NOALIGN PART_T;

static PPART_T Partition;

Code: Select all

byte Check_PartTable(int ide,int drive,int part)
{
   PART_T PartTable;
   int i;
   if(Read_ones_CHS(0,0,drive,ide,1,0x20,(word*)   &PartTable)!=0)
      {return 1;}
   kprintf("Sig: 0x%x\n Active1: 0x%x\n Sect1: 0x%   x\n HeadCyl1: 0x%x\n"
            " Part1: 0x%x\n ESect1: 0x%   x\n EHeadCyl1: 0x%x\n " 
            "SLBA1: 0x%x\n Size1:0x%   x\n",
            PartTable.Sig,PartTable.Active1,PartTable.Sect1,
            Part   Table.HeadCyl1,PartTable.Part1,PartTable.ESect1,
            Par   tTable.EHeadCyl1,PartTable.SLBA1,PartTable.Size1
         );
}

Code: Select all

word Read_ones_CHS(int cyln,int head,int drive,int ide,word sect,int com,word *buffer)
{
   int r0,r1,r2,i;
   if(head>15)
      r1=15;
   else if(head<0)
      r1=0;
   else
      r1=head;
   if(drive>1)
      r2=16;
   if(drive<0)
      r2=0;
   if(drive==1)
      r2=16;
   while(!inportb(ide+7)&0xD0);
   outportb(ide+6,(r1|r2|0xA0));
   outportb(ide+2,0x01);
   outportb(ide+3,sect);
   outportb(ide+4,cyln&0xFF);
   outportb(ide+5,cyln>>8);
   outportb(ide+7,com);
   while((inportb(ide+7)&0x50)!=0x50);
   while((inportb(ide+7)&0x08)!=0x08);
   while((inportb(ide+7)&0x80)==0x80);
   for(i=0;i<256;i++)
   {
      r0=inportw(ide);
      buffer[i]=(r0>>8)|(r0&0xFF)<<8;
   }
   while(inportb(ide+7)&0x80);
   outportb(ide+0x206,0x12);
   return inportb(ide+1);
}
THX in advance
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Partition Table Woes :'(

Post by Pype.Clicker »

If i were you, i wouldn't do such a large structure for the partition table: i'd rather write a small structure that describes a partition entry and then use that structure with

Code: Select all

byte buffer[512];
struct MBR_entry *partition1 = (struct MBR_entry*)(buffer+446); // (should be 510-4*16 (?))
struct MBR_entry *partition1 = partition1+1;
...
SystemHalted

Re:Partition Table Woes :'(

Post by SystemHalted »

I used the large struct as a quick test to see if I could read proporly from the partition table but the data I receive is weird
i've tried it on a lot of computers now and its always the same shoul I just continue and use the LBA values to create CHS values?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Partition Table Woes :'(

Post by Pype.Clicker »

hmm ... i'm not sure it'd be wise, except if you find something saying to do so in some doc.

When you say that the 'values are weird', how do you decode them ? iirc, the BIOS was doing quite nasty packing like placing high bits of 'head' in the sector and things alike...
SystemHalted

Re:Partition Table Woes :'(

Post by SystemHalted »

its totally different then what the docs say
their layout:
80 Active flag
?? Chs suff
??
??
20 Partition type 20 for my part
?? End CHS stuff
??
??
?? LBA Start
??
??
??
?? LBA Size
??
??
??

??=Values that varie

The layout I get:
01 ??? always 1 or 0
80Active Flag
??
??
??
20 Part num
??
??
??LBASect
??
??
??
??LBASize
??
??
??
Dreamsmith

Re:Partition Table Woes :'(

Post by Dreamsmith »

You're reversing the order of the bytes in the words you're reading from the interface. Don't.

More specifically, replace this code, which scrambles data as it reads it:

Code: Select all

  for(i=0;i<256;i++)
  {
      r0=inportw(ide);
      buffer[i]=(r0>>8)|(r0&0xFF)<<8;
  }
With this code:

Code: Select all

  for(i=0;i<256;i++)
    buffer[i]=inportw(ide);
SystemHalted

Re:Partition Table Woes :'(

Post by SystemHalted »

Great Thanx I'll try it

Oh and enything else i read was in reverse order so i assumed this would be too ::)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Partition Table Woes :'(

Post by Pype.Clicker »

well, the data in the ATA identify packet are big-endian (reverse ordered words) because ATA standardization said it'd be so, but the partition tables are more ancient than this and are to be read the same way whether they come from an ATA disk or not ;)
Dreamsmith

Re:Partition Table Woes :'(

Post by Dreamsmith »

Pype.Clicker wrote: well, the data in the ATA identify packet are big-endian (reverse ordered words) because ATA standardization said it'd be so, but the partition tables are more ancient than this and are to be read the same way whether they come from an ATA disk or not ;)
Actually, it's a little more bizarre than that. The serial number (10), firmware revision (23), and model number (27) strings come in from the ATA identify packet in byte swapped order, but only those three strings come in with their bytes out of order. For example, the cylinders, heads and sectors at words 1, 3, and 6 (or 54, 55 and 56 if the low bit of word 53 is set) are read with a simple inportw, no byte swapping required.
SystemHalted

Re:Partition Table Woes :'(

Post by SystemHalted »

Wow thats bizzare, so when writing a file system I should not worry about about byte swapping or those other non-byte swapped areas or check for the non byte swaped areas

5 min after post:
Lol my floppy drive broke guess i'm gunna have a hard time testing my os unless i can fix it. ::)
[glow=red,2,300]SystemHalted[/glow]
Post Reply