bug-grub
[Top][All Lists]

## Re: "partnew" Command Writes Wrong Ending Cylinder in MPT

 From: sburtchin Subject: Re: "partnew" Command Writes Wrong Ending Cylinder in MPT Date: Tue, 21 Nov 2006 03:04:15 -0800 (PST)

```
>
> What is the algorigthm that partnew should do in your opinnion.
>
> I mean given a partition type, its start and its length how do you
> calculate the ending cylinder field ?
>
Well to not confuse the matter (my last reply makes this look far more
complicated than it actual is), I should do some summarizing and point out
that the same algorithm works to calculate both the starting and ending
values.

The "partnew" cammand accepts four arguments.
1>The first (eg. "(hd0,0)") specifies the offset in the MBR where writing
should begin.
2>The second (eg. "0x0F") specifies the filesystem type.  This is written to
the partition table without translation.
3>The third (eg. "2056320") specifies the LBA of the first sector in the
partition.
4>The fourth (eg. "158019120") specifies the number of sectors in the
partition.

(eg. my 80GB disk has cylinders/heads/sectors = 10587/240/63)

Next calculate the starting and ending LBA's for the partition:
StartLBA = <third argument>                        (eg. "2056320")
EndLBA = StartLBA + <fourth argument> - 1   (eg. "2056320 + 158019120 - 1"
or "160075439")

Then apply the following algorithm to both the StartLBA and EndLBA to find
CHS values to write to disk:
Cyl=Min(RealCyl,1023)

Plugging in the numbers to calculate start C/H/S (LBA=StartLBA):
RealCyl=Integer(2056320/(240*63)) = 136
Cyl=Min(136,1023) = 136
Hd= Integer(2056320/63) - 136*240 = 0
Sec=mod[240*63](2056320) - 0*63 + 1 = 1

To calculate end C/H/S (LBA=EndLBA):
RealCyl=Integer(160075439/(240*63)) = 10586
Cyl=Min(10586,1023) = 1023
Hd= Integer(160075439/63) - 10586*240 = 239
Sec=mod[240*63](160075439) - 239*63 + 1 = 63

I got some really strange results with partnew when I plugged in values that
did not begin/end on cylinder boundaries, so I think the algorithm in the
code you provided is considerably different than above.  I will need to
brush up on C to make a proper analysis.  Also as said earlier, partnew
seems to not to be able to write a start or end cylinder greater than 2 less
than the max allowed for that disk (eg. on my disk with 833 cylinders, a
real start or end cylinder value of "831" would be written as "830", as
would a real start or end value of "832".  But for real values <= 830 the
correct value is written.  Likewise, for my disk with 10587 cylinders, for
values <= 1021 the correct value is written, but above that "1021" is always
written.

sburtchin wrote:
>
>  I don't know if it matters in actual practice, but "partnew (hd0,3) 0x00
> 0 0" should write C/H/S 0/0/0 thru 0/0/0
>
Well, I may have assumed too much here.  What I am trying to say is that
there are situations where it is highly desirable to make a partition appear
not to exist (eg. when installing Microsoft operating systems, and in
practice too).  (unused partition table entries are supposed to be all
zeroes)  This is most effectively achieved by zeroing out the entry for it
in the MPT (as opposed to just changing the filesystem type ["hiding"] to
make it look like an unrecognized filesystem).  There are tools for doing
this, but for efficiency it would be very desireable to be able to do this
from GRUB.  There are two obvious ways to achieve this: 1) partnew could
test the fourth argument to see if it is equal to zero (ie. a zero length
partition), and if true then write all zeroes to that entry in the MPT OR 2)
partnew could look for a command line switch, and if found write all zeroes
to that entry in the MPT (eg. "partnew (hd0,3) -BLANK").  Obviously, "1)" is
very easy to implement and "2)" is more user friendly, but probably more
work.
--
View this message in context:
http://www.nabble.com/%22partnew%22-Command-Writes-Wrong-Ending-Cylinder-in-MPT-tf2599372.html#a7469382
Sent from the Grub - Bugs mailing list archive at Nabble.com.

```