2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
5 /******************************************************************************
14 DEVICE/A, UNIT/N/K/A, PARTITIONNUMBER=PN/K/N, GRUB/K/A, FORCELBA/S
22 Installs the GRUB 2 bootloader to the boot block of the specified
27 DEVICE -- Device name (e.g. ata.device)
29 PN -- Specifies a partition number. If specified, GRUB is installed
30 to this partition's boot block. Otherwise, GRUB is installed to
31 the disk's boot block.
32 GRUB -- Path to GRUB directory.
33 FORCELBA -- Force use of LBA mode.
41 Install-grub2-i386-pc DEVICE ata.device UNIT 0 GRUB DH0:boot/grub
47 Partition, SYS:System/Format
51 ******************************************************************************/
54 #include <aros/debug.h>
57 #include <proto/exec.h>
58 #include <proto/dos.h>
59 #include <proto/partition.h>
60 #include <proto/utility.h>
61 #include <aros/macros.h>
62 #include <devices/hardblocks.h>
63 #include <devices/newstyle.h>
64 #include <exec/errors.h>
65 #include <exec/memory.h>
66 #include <libraries/partition.h>
70 /* Defines for grub2 data */
71 /* boot.img pointers */
72 #define GRUB_BOOT_MACHINE_BPB_START 0x03
73 #define GRUB_BOOT_MACHINE_BPB_END 0x5a
74 #define GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC 0x01b8 /* Following grub2 grub-setup sources */
75 #define GRUB_BOOT_MACHINE_PART_START 0x01be
76 #define GRUB_BOOT_MACHINE_PART_END 0x01fe
77 #define GRUB_BOOT_MACHINE_KERNEL_SECTOR 0x5c
78 #define GRUB_BOOT_MACHINE_BOOT_DRIVE 0x64
79 #define GRUB_BOOT_MACHINE_DRIVE_CHECK 0x66
81 /* core.img pointers */
82 #define GRUB_DECOMPRESSOR_I386_PC_BOOT_DEVICE 0x18
85 #define BIOS_HDISK_FLAG 0x80
87 #define MBR_MAX_PARTITIONS 4
88 #define MBRT_EXTENDED 0x05
89 #define MBRT_EXTENDED2 0x0f
90 #define BLCKLIST_ELEMENTS 14
109 #define VF_IS_TRACKDISK (1<<0)
110 #define VF_IS_RDB (1<<1)
120 const TEXT version
[] = "$VER: Install-grub2-i386-pc 41.4 (15.9.2012)";
122 CONST_STRPTR CORE_IMG_FILE_NAME
= "i386-pc/core.img";
125 (STRPTR
) ("DEVICE/A," "UNIT/N/K/A," "PARTITIONNUMBER=PN/K/N," "GRUB/K/A,"
128 IPTR myargs
[7] = { 0, 0, 0, 0, 0, 0 };
130 struct FileSysStartupMsg
*getDiskFSSM(CONST_STRPTR path
)
133 struct DeviceNode
*dn
;
137 D(bug("[install] getDiskFSSM('%s')\n", path
));
139 for (i
= 0; (path
[i
]) && (path
[i
] != ':'); i
++)
144 dl
= LockDosList(LDF_READ
);
147 dn
= (struct DeviceNode
*) FindDosEntry(dl
, dname
, LDF_DEVICES
);
148 UnLockDosList(LDF_READ
);
153 if (IsFileSystem(dname
))
155 return (struct FileSysStartupMsg
*) BADDR(dn
->dn_Startup
);
158 Printf("device '%s' doesn't contain a file system\n",
162 PrintFault(ERROR_OBJECT_NOT_FOUND
, dname
);
166 Printf("'%s' doesn't contain a device name\n", path
);
170 void fillGeometry(struct Volume
*volume
, struct DosEnvec
*de
)
174 D(bug("[install] fillGeometry(%x)\n", volume
));
176 spc
= de
->de_Surfaces
* de
->de_BlocksPerTrack
;
177 volume
->SizeBlock
= de
->de_SizeBlock
;
178 volume
->startblock
= de
->de_LowCyl
* spc
;
180 ((de
->de_HighCyl
- de
->de_LowCyl
+ 1) * spc
) - 1 + de
->de_Reserved
;
183 void nsdCheck(struct Volume
*volume
)
185 struct NSDeviceQueryResult nsdq
;
188 D(bug("[install] nsdCheck(%x)\n", volume
));
190 if (((volume
->startblock
+ volume
->countblock
) * /* last block */
191 ((volume
->SizeBlock
<< 2) / 512) /* 1 portion (block) equals 512 (bytes) */
194 nsdq
.SizeAvailable
= 0;
195 nsdq
.DevQueryFormat
= 0;
196 volume
->iotd
->iotd_Req
.io_Command
= NSCMD_DEVICEQUERY
;
197 volume
->iotd
->iotd_Req
.io_Data
= &nsdq
;
198 volume
->iotd
->iotd_Req
.io_Length
= sizeof(struct NSDeviceQueryResult
);
199 if (DoIO((struct IORequest
*) &volume
->iotd
->iotd_Req
) == IOERR_NOCMD
)
201 Printf("Device doesn't understand NSD-Query\n");
205 if ((volume
->iotd
->iotd_Req
.io_Actual
>
206 sizeof(struct NSDeviceQueryResult
))
207 || (volume
->iotd
->iotd_Req
.io_Actual
== 0)
208 || (volume
->iotd
->iotd_Req
.io_Actual
!= nsdq
.SizeAvailable
))
210 Printf("WARNING wrong io_Actual using NSD\n");
214 if (nsdq
.DeviceType
!= NSDEVTYPE_TRACKDISK
)
215 Printf("WARNING no trackdisk type\n");
216 for (cmdcheck
= nsdq
.SupportedCommands
; *cmdcheck
; cmdcheck
++)
218 if (*cmdcheck
== NSCMD_TD_READ64
)
219 volume
->readcmd
= NSCMD_TD_READ64
;
220 if (*cmdcheck
== NSCMD_TD_WRITE64
);
221 volume
->writecmd
= NSCMD_TD_WRITE64
;
223 if ((volume
->readcmd
!= NSCMD_TD_READ64
) ||
224 (volume
->writecmd
!= NSCMD_TD_WRITE64
))
225 Printf("WARNING no READ64/WRITE64\n");
231 struct Volume
*initVolume(CONST_STRPTR device
, ULONG unit
, ULONG flags
,
234 struct Volume
*volume
;
237 D(bug("[install] initVolume(%s:%d)\n", device
, unit
));
239 volume
= AllocVec(sizeof(struct Volume
), MEMF_PUBLIC
| MEMF_CLEAR
);
242 volume
->mp
= CreateMsgPort();
246 (struct IOExtTD
*) CreateIORequest(volume
->mp
,
247 sizeof(struct IOExtTD
));
250 volume
->blockbuffer
=
251 AllocVec(de
->de_SizeBlock
<< 2, MEMF_PUBLIC
| MEMF_CLEAR
);
252 if (volume
->blockbuffer
)
256 unit
, (struct IORequest
*) volume
->iotd
, flags
) == 0)
258 if (strcmp((const char *) device
, TD_NAME
) == 0)
259 volume
->flags
|= VF_IS_TRACKDISK
;
261 volume
->flags
|= VF_IS_RDB
; /* just assume we have RDB */
262 volume
->readcmd
= CMD_READ
;
263 volume
->writecmd
= CMD_WRITE
;
264 volume
->device
= device
;
265 volume
->unitnum
= unit
;
267 fillGeometry(volume
, de
);
272 error
= ERROR_NO_FREE_STORE
;
273 FreeVec(volume
->blockbuffer
);
276 error
= ERROR_NO_FREE_STORE
;
277 DeleteIORequest((struct IORequest
*) volume
->iotd
);
280 error
= ERROR_NO_FREE_STORE
;
281 DeleteMsgPort(volume
->mp
);
284 error
= ERROR_NO_FREE_STORE
;
288 error
= ERROR_NO_FREE_STORE
;
290 PrintFault(error
, NULL
);
294 void uninitVolume(struct Volume
*volume
)
296 D(bug("[install] uninitVolume(%x)\n", volume
));
298 CloseDevice((struct IORequest
*) volume
->iotd
);
299 FreeVec(volume
->blockbuffer
);
300 DeleteIORequest((struct IORequest
*) volume
->iotd
);
301 DeleteMsgPort(volume
->mp
);
305 static ULONG
_readwriteBlock(struct Volume
*volume
,
306 ULONG block
, APTR buffer
, ULONG length
,
312 volume
->iotd
->iotd_Req
.io_Command
= command
;
313 volume
->iotd
->iotd_Req
.io_Length
= length
;
314 volume
->iotd
->iotd_Req
.io_Data
= buffer
;
315 offset
= (UQUAD
) (volume
->startblock
+ block
) * (volume
->SizeBlock
<< 2);
316 volume
->iotd
->iotd_Req
.io_Offset
= offset
& 0xFFFFFFFF;
317 volume
->iotd
->iotd_Req
.io_Actual
= offset
>> 32;
318 retval
= DoIO((struct IORequest
*) &volume
->iotd
->iotd_Req
);
319 if (volume
->flags
& VF_IS_TRACKDISK
)
321 volume
->iotd
->iotd_Req
.io_Command
= TD_MOTOR
;
322 volume
->iotd
->iotd_Req
.io_Length
= 0;
323 DoIO((struct IORequest
*) &volume
->iotd
->iotd_Req
);
328 ULONG
readBlock(struct Volume
* volume
, ULONG block
, APTR buffer
, ULONG size
)
330 D(bug("[install] readBlock(vol:%x, block:%d, %d bytes)\n",
331 volume
, block
, size
));
333 return _readwriteBlock(volume
, block
, buffer
, size
, volume
->readcmd
);
336 ULONG
writeBlock(struct Volume
* volume
, ULONG block
, APTR buffer
, ULONG size
)
338 D(bug("[install] writeBlock(vol:%x, block:%d, %d bytes)\n",
339 volume
, block
, size
));
341 return _readwriteBlock(volume
, block
, buffer
, size
, volume
->writecmd
);
344 static BOOL
isKnownFs(ULONG dos_id
)
349 case ID_INTER_DOS_DISK
:
350 case ID_INTER_FFS_DISK
:
351 case ID_FASTDIR_DOS_DISK
:
352 case ID_FASTDIR_FFS_DISK
:
361 BOOL
isvalidFileSystem(struct Volume
* volume
, CONST_STRPTR device
,
365 struct PartitionBase
*PartitionBase
;
366 struct PartitionHandle
*ph
;
369 D(bug("[install] isvalidFileSystem(%x, %s, %d)\n", volume
, device
, unit
));
371 if (readBlock(volume
, 0, volume
->blockbuffer
, 512))
373 Printf("Read Error\n");
377 dos_id
= AROS_BE2LONG(volume
->blockbuffer
[0]);
379 if (!isKnownFs(dos_id
))
381 /* first block has no DOS\x so we don't have RDB for sure */
382 volume
->flags
&= ~VF_IS_RDB
;
383 if (readBlock(volume
, 1, volume
->blockbuffer
, 512))
385 Printf("Read Error\n");
389 dos_id
= AROS_BE2LONG(volume
->blockbuffer
[0]);
391 if (!isKnownFs(dos_id
))
394 volume
->dos_id
= dos_id
;
397 volume
->dos_id
= dos_id
;
399 volume
->partnum
= -1;
402 (struct PartitionBase
*) OpenLibrary((CONST_STRPTR
)
403 "partition.library", 1);
406 ph
= OpenRootPartition(device
, unit
);
409 if (OpenPartitionTable(ph
) == 0)
411 struct TagItem tags
[3];
414 tags
[1].ti_Tag
= TAG_DONE
;
415 tags
[0].ti_Tag
= PTT_TYPE
;
416 tags
[0].ti_Data
= (STACKIPTR
) & type
;
417 GetPartitionTableAttrs(ph
, tags
);
418 if (type
== PHPTT_MBR
)
420 struct PartitionHandle
*pn
;
422 struct PartitionHandle
*extph
= NULL
;
423 struct PartitionType ptype
= { };
425 tags
[0].ti_Tag
= PT_DOSENVEC
;
426 tags
[0].ti_Data
= (STACKIPTR
) & de
;
427 tags
[1].ti_Tag
= PT_TYPE
;
428 tags
[1].ti_Data
= (STACKIPTR
) & ptype
;
429 tags
[2].ti_Tag
= TAG_DONE
;
430 pn
= (struct PartitionHandle
*) ph
->table
->list
.lh_Head
;
431 while (pn
->ln
.ln_Succ
)
435 GetPartitionAttrs(pn
, tags
);
436 if (ptype
.id
[0] == MBRT_EXTENDED
437 || ptype
.id
[0] == MBRT_EXTENDED2
)
441 scp
= de
.de_Surfaces
* de
.de_BlocksPerTrack
;
442 if ((volume
->startblock
>= (de
.de_LowCyl
* scp
))
443 && (volume
->startblock
<=
444 (((de
.de_HighCyl
+ 1) * scp
) - 1)))
447 pn
= (struct PartitionHandle
*) pn
->ln
.ln_Succ
;
451 tags
[0].ti_Tag
= PT_POSITION
;
452 tags
[0].ti_Data
= (STACKIPTR
) & type
;
453 tags
[1].ti_Tag
= TAG_DONE
;
454 GetPartitionAttrs(pn
, tags
);
455 volume
->partnum
= (UBYTE
) type
;
458 ("[install] Primary partition found: partnum=%d\n",
461 else if (extph
!= NULL
)
463 if (OpenPartitionTable(extph
) == 0)
465 tags
[0].ti_Tag
= PTT_TYPE
;
466 tags
[0].ti_Data
= (STACKIPTR
) & type
;
467 tags
[1].ti_Tag
= TAG_DONE
;
468 GetPartitionTableAttrs(extph
, tags
);
469 if (type
== PHPTT_EBR
)
471 tags
[0].ti_Tag
= PT_DOSENVEC
;
472 tags
[0].ti_Data
= (STACKIPTR
) & de
;
473 tags
[1].ti_Tag
= TAG_DONE
;
474 pn
= (struct PartitionHandle
*) extph
->table
->
476 while (pn
->ln
.ln_Succ
)
480 offset
= extph
->de
.de_LowCyl
481 * extph
->de
.de_Surfaces
482 * extph
->de
.de_BlocksPerTrack
;
483 GetPartitionAttrs(pn
, tags
);
485 de
.de_Surfaces
* de
.de_BlocksPerTrack
;
486 if ((volume
->startblock
>=
487 offset
+ (de
.de_LowCyl
* scp
))
488 && (volume
->startblock
<=
490 (((de
.de_HighCyl
+ 1) * scp
) -
493 pn
= (struct PartitionHandle
*) pn
->ln
.
498 tags
[0].ti_Tag
= PT_POSITION
;
499 tags
[0].ti_Data
= (STACKIPTR
) & type
;
500 GetPartitionAttrs(pn
, tags
);
502 MBR_MAX_PARTITIONS
+ (UBYTE
) type
;
505 ("[install] Logical partition found: partnum=%d\n",
506 (int) volume
->partnum
));
509 ClosePartitionTable(extph
);
515 if (type
== PHPTT_RDB
)
517 /* just use whole hard disk */
522 ("only MBR and RDB partition tables are supported\n");
524 ClosePartitionTable(ph
);
528 /* just use whole hard disk */
531 CloseRootPartition(ph
);
534 Printf("Error OpenRootPartition(%s,%lu)\n", device
, (long)unit
);
535 CloseLibrary((struct Library
*) PartitionBase
);
538 Printf("Couldn't open partition.library\n");
542 struct Volume
*getGrubStageVolume(CONST_STRPTR device
, ULONG unit
,
543 ULONG flags
, struct DosEnvec
*de
)
545 struct Volume
*volume
;
547 volume
= initVolume(device
, unit
, flags
, de
);
549 D(bug("[install] getGrubStageVolume(): volume=%x\n", volume
));
553 if (isvalidFileSystem(volume
, device
, unit
))
557 Printf("stage2 is on an unsupported file system\n");
558 PrintFault(ERROR_OBJECT_WRONG_TYPE
, NULL
);
560 uninitVolume(volume
);
565 BOOL
isvalidPartition(CONST_STRPTR device
, ULONG unit
, LONG
* pnum
,
566 struct DosEnvec
* de
)
568 struct PartitionBase
*PartitionBase
;
569 struct PartitionHandle
*ph
;
574 ("[install] isvalidPartition(%s:%d, part:%d)\n", device
, unit
, pnum
));
577 (struct PartitionBase
*) OpenLibrary((CONST_STRPTR
)
578 "partition.library", 1);
581 ph
= OpenRootPartition(device
, unit
);
584 struct TagItem tags
[2];
586 tags
[1].ti_Tag
= TAG_DONE
;
587 /* is there a partition table? */
588 if (OpenPartitionTable(ph
) == 0)
592 /* install into partition bootblock */
593 tags
[0].ti_Tag
= PTT_TYPE
;
594 tags
[0].ti_Data
= (STACKIPTR
) & type
;
595 GetPartitionTableAttrs(ph
, tags
);
596 if (type
== PHPTT_MBR
)
598 struct PartitionHandle
*pn
;
600 /* search for partition */
601 tags
[0].ti_Tag
= PT_POSITION
;
602 tags
[0].ti_Data
= (STACKIPTR
) & type
;
603 pn
= (struct PartitionHandle
*) ph
->table
->list
.
605 while (pn
->ln
.ln_Succ
)
607 GetPartitionAttrs(pn
, tags
);
610 pn
= (struct PartitionHandle
*) pn
->ln
.ln_Succ
;
614 struct PartitionType ptype
;
616 /* is it an AROS partition? */
617 tags
[0].ti_Tag
= PT_TYPE
;
618 tags
[0].ti_Data
= (STACKIPTR
) & ptype
;
619 GetPartitionAttrs(pn
, tags
);
620 if (ptype
.id
[0] == 0x30)
622 tags
[0].ti_Tag
= PT_DOSENVEC
;
623 tags
[0].ti_Data
= (STACKIPTR
) de
;
624 GetPartitionAttrs(pn
, tags
);
629 ("partition is not of type AROS (0x30)\n");
634 ("partition %ld not found on device %s unit %lu\n",
635 (long)*pnum
, device
, (long)unit
);
640 ("you can only install in partitions which are MBR partitioned\n");
644 /* install into MBR */
645 tags
[0].ti_Tag
= PTT_TYPE
;
646 tags
[0].ti_Data
= (STACKIPTR
) & type
;
647 GetPartitionTableAttrs(ph
, tags
);
648 if ((type
== PHPTT_MBR
) || (type
== PHPTT_RDB
))
650 tags
[0].ti_Tag
= PT_DOSENVEC
;
651 tags
[0].ti_Data
= (STACKIPTR
) de
;
652 GetPartitionAttrs(ph
, tags
);
657 ("partition table type must be either MBR or RDB\n");
659 ClosePartitionTable(ph
);
663 /* FIXME: GetPartitionAttr() should always work for root partition */
664 CopyMem(&ph
->de
, de
, sizeof(struct DosEnvec
));
667 CloseRootPartition(ph
);
670 Printf("Error OpenRootPartition(%s,%lu)\n", device
, (long)unit
);
671 CloseLibrary((struct Library
*) PartitionBase
);
674 Printf("Couldn't open partition.library\n");
678 struct Volume
*getBBVolume(CONST_STRPTR device
, ULONG unit
, LONG
* partnum
)
680 struct Volume
*volume
;
683 D(bug("[install] getBBVolume(%s:%d, %d)\n", device
, unit
, partnum
));
685 if (isvalidPartition(device
, unit
, partnum
, &de
))
687 volume
= initVolume(device
, unit
, 0, &de
);
688 volume
->partnum
= partnum
? *partnum
: -1;
689 readBlock(volume
, 0, volume
->blockbuffer
, 512);
690 if (AROS_BE2LONG(volume
->blockbuffer
[0]) != IDNAME_RIGIDDISK
)
692 /* Clear the boot sector region! */
693 memset(volume
->blockbuffer
, 0x00, 446);
697 Printf("no space for bootblock (RDB is on block 0)\n");
702 /* Convert a unit number into a drive number as understood by GRUB */
703 UWORD
getDriveNumber(CONST_STRPTR device
, ULONG unit
)
705 struct PartitionHandle
*ph
;
709 for (i
= 0; i
< unit
; i
++)
711 ph
= OpenRootPartition(device
, i
);
715 CloseRootPartition(ph
);
722 BOOL
writeBootIMG(STRPTR bootimgpath
, struct Volume
* bootimgvol
, struct Volume
* coreimgvol
,
723 ULONG block
/* first block of core.img file */, ULONG unit
)
729 D(bug("[install] writeBootIMG(%x)\n", bootimgvol
));
731 fh
= Open(bootimgpath
, MODE_OLDFILE
);
734 if (Read(fh
, bootimgvol
->blockbuffer
, 512) == 512)
736 /* install into MBR ? */
737 if ((bootimgvol
->startblock
== 0)
738 && (!(bootimgvol
->flags
& VF_IS_TRACKDISK
)))
740 APTR boot_img
= bootimgvol
->blockbuffer
;
743 (UBYTE
*) (boot_img
+ GRUB_BOOT_MACHINE_BOOT_DRIVE
);
744 UWORD
*boot_drive_check
=
745 (UWORD
*) (boot_img
+ GRUB_BOOT_MACHINE_DRIVE_CHECK
);
747 if (unit
== bootimgvol
->unitnum
)
750 *boot_drive
= getDriveNumber(coreimgvol
->device
, unit
)
752 *boot_drive_check
= 0x9090;
754 D(bug("[install] writeBootIMG: Install to HARDDISK\n"));
757 error
= readBlock(bootimgvol
, 0, coreimgvol
->blockbuffer
, 512);
759 D(bug("[install] writeBootIMG: MBR Buffer @ %x\n", bootimgvol
->blockbuffer
));
760 D(bug("[install] writeBootIMG: Copying MBR BPB to %x\n",
761 (char *) bootimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_BPB_START
));
762 /* copy BPB (BIOS Parameter Block) */
764 ((APTR
) ((char *) coreimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_BPB_START
),
765 (APTR
) ((char *) bootimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_BPB_START
),
766 (GRUB_BOOT_MACHINE_BPB_END
- GRUB_BOOT_MACHINE_BPB_START
));
768 /* copy partition table - [Overwrites Floppy boot code] */
769 D(bug("[install] writeBootIMG: Copying MBR Partitions to %x\n",
770 (char *) bootimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
));
771 CopyMem((APTR
) ((char *) coreimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
),
772 (APTR
) ((char *) bootimgvol
->blockbuffer
+ GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
),
773 (GRUB_BOOT_MACHINE_PART_END
- GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
));
775 /* Store the core.img pointer .. */
776 ULONG
* coreimg_sector_start
= (ULONG
*) (boot_img
777 + GRUB_BOOT_MACHINE_KERNEL_SECTOR
);
778 coreimg_sector_start
[0] = block
;
779 D(bug("[install] writeBootIMG: core.img pointer = %ld\n", block
));
783 D(bug("[install] writeBootIMG: Install to FLOPPY\n"));
788 error
= writeBlock(bootimgvol
, 0, bootimgvol
->blockbuffer
, 512);
791 Printf("WriteError %lu\n", (long)error
);
796 Printf("WriteError %lu\n", (long)error
);
799 Printf("%s: Read Error\n", bootimgpath
);
803 PrintFault(IoErr(), bootimgpath
);
808 /* Collects the list of blocks that a file occupies on FFS filesystem */
809 ULONG
collectBlockListFFS(struct Volume
*volume
, ULONG block
, struct BlockNode
*blocklist
)
811 ULONG retval
, first_block
;
812 WORD blk_count
,count
;
815 D(bug("[install] collectBlockListFFS(%x, %ld, %x)\n", volume
, block
, blocklist
));
818 /* Clear the core.img sector pointers region! */
819 memset((UBYTE
*)&blocklist
[-BLCKLIST_ELEMENTS
],0x00, BLCKLIST_ELEMENTS
*sizeof(struct BlockNode
));
822 The number of first block of core.img will be stored in boot.img
823 so skip the first filekey in the first loop
826 retval
= _readwriteBlock(volume
, block
, volume
->blockbuffer
, volume
->SizeBlock
<<2,
831 D(bug("[install] collectBlockListFFS: ERROR reading block (error: %ld\n", retval
));
832 Printf("ReadError %lu\n", (long)retval
);
836 i
= volume
->SizeBlock
- 52;
837 first_block
= AROS_BE2LONG(volume
->blockbuffer
[volume
->SizeBlock
-51]);
840 D(bug("[install] collectBlockListFFS: First block @ %ld, i:%d\n", first_block
, i
));
845 retval
= _readwriteBlock(volume
, block
, volume
->blockbuffer
, volume
->SizeBlock
<<2,
849 D(bug("[install] collectBlockListFFS: ERROR reading block (error: %ld)\n", retval
));
850 Printf("ReadError %lu\n", (long)retval
);
854 D(bug("[install] collectBlockListFFS: read block %ld, i = %d\n", block
, i
));
855 while ((i
>=6) && (volume
->blockbuffer
[i
]))
857 D(bug("[install] collectBlockListFFS: i = %d\n", i
));
859 if current sector follows right after last sector
860 then we don't need a new element
862 if ((blocklist
[blk_count
].sector_lo
) &&
863 ((blocklist
[blk_count
].sector_lo
+blocklist
[blk_count
].count
)==
864 AROS_BE2LONG(volume
->blockbuffer
[i
])))
866 blocklist
[blk_count
].count
+= 1;
867 D(bug("[install] collectBlockListFFS: sector %d follows previous - increasing count of block %d to %d\n",
868 i
, blk_count
, blocklist
[blk_count
].count
));
872 blk_count
--; /* decrement first */
873 D(bug("[install] collectBlockListFFS: store new block (%d)\n", blk_count
));
875 if ((blk_count
-1) <= -BLCKLIST_ELEMENTS
)
877 D(bug("[install] collectBlockListFFS: ERROR: out of block space at sector %d, block %d\n",
879 Printf("There is no more space to save blocklist in core.img\n");
882 D(bug("[install] collectBlockListFFS: storing sector pointer for %d in block %d\n",
884 blocklist
[blk_count
].sector_lo
= AROS_BE2LONG(volume
->blockbuffer
[i
]);
885 blocklist
[blk_count
].sector_hi
= 0;
886 blocklist
[blk_count
].count
= 1;
890 i
= volume
->SizeBlock
- 51;
891 block
= AROS_BE2LONG(volume
->blockbuffer
[volume
->SizeBlock
- 2]);
892 D(bug("[install] collectBlockListFFS: next block %d, i = %d\n", block
, i
));
897 blocks in blocklist are relative to the first
898 sector of the HD (not partition)
901 D(bug("[install] collectBlockListFFS: successfully updated pointers for %d blocks\n", blk_count
));
904 for (count
=-1;count
>=blk_count
;count
--)
906 blocklist
[count
].sector_lo
+= volume
->startblock
;
907 blocklist
[count
].seg_adr
= 0x820 + (i
*32);
908 i
+= blocklist
[count
].count
;
909 D(bug("[install] collectBlockListFFS: correcting block %d for partition start\n", count
));
910 D(bug("[install] collectBlockListFFS: sector : %ld seg_adr : %x\n",
911 blocklist
[count
].sector_lo
, blocklist
[count
].seg_adr
));
914 first_block
+= volume
->startblock
;
915 D(bug("[install] collectBlockListFFS: corrected first block for partition start: %ld\n", first_block
));
920 /* Collects the list of blocks that a file occupies on SFS filesystem */
921 ULONG
collectBlockListSFS(struct Volume
*volume
, ULONG objectnode
, struct BlockNode
*blocklist
)
923 ULONG retval
, first_block
= 0;
924 WORD blk_count
= 0, count
= 0;
925 ULONG block_objectnoderoot
= 0, block_sfsobjectcontainer
= 0, block_extentbnoderoot
= 0;
926 ULONG nextblock
= 0, searchedblock
= 0;
928 UBYTE
* tmpBytePtr
= NULL
;
930 D(bug("[install] collectBlockListSFS(startblock: %ld, objectnode: %ld)\n", volume
->startblock
, objectnode
));
931 D(bug("[install] collectBlockListSFS(%ld, %d, %d)\n", volume
->countblock
, volume
->SizeBlock
, volume
->partnum
));
933 /* Clear the core.img sector pointers region! */
934 memset((UBYTE
*)&blocklist
[-BLCKLIST_ELEMENTS
],0x00, BLCKLIST_ELEMENTS
*sizeof(struct BlockNode
));
936 /* Description of actions:
937 * 1. Load SFS root block
938 * 2. From root block find the block containing root of objectnodes
939 * 3. Traverse the tree of objectnodes until block of objectdescriptor is found
940 * 4. Search the objectdescriptor for entry matching given objectnode from entry read the
941 * first block of file
942 * 5. Having first file block, find the extentbnode for that block and read number
943 * of blocks. Put first block and number of blocks into BlockList.
944 * 6. If the file has more blocks than this exntentbnode hold, find first file
945 * block in next extentbnode. Go to step 5.
946 * Use the SFS source codes for reference. They operate on structures not pointers
947 * and are much easier to understand.
950 /* Read root block */
951 retval
= _readwriteBlock(volume
, 0, volume
->blockbuffer
, volume
->SizeBlock
<<2,
956 D(bug("[install] collectBlockListSFS: ERROR reading root block (error: %ld)\n", retval
));
957 Printf("ReadError %lu\n", (long)retval
);
961 /* Get block pointers from root block */
962 block_objectnoderoot
= AROS_BE2LONG(volume
->blockbuffer
[28]); /* objectnoderoot - 29th ULONG */
963 block_extentbnoderoot
= AROS_BE2LONG(volume
->blockbuffer
[27]); /* extentbnoderoot - 28th ULONG */
965 D(bug("[install] collectBlockListSFS: objectnoderoot: %ld, extentbnoderoot %ld\n",
966 block_objectnoderoot
, block_extentbnoderoot
));
970 /* Find the SFSObjectContainer block for given objectnode */
971 /* Reference: SFS, nodes.c, function findnode */
972 nextblock
= block_objectnoderoot
;
973 D(bug("[install] collectBlockListSFS: searching in nextblock %d for sfsobjectcontainer for objectnode %ld\n",
974 nextblock
, objectnode
));
977 _readwriteBlock(volume
, nextblock
, volume
->blockbuffer
, volume
->SizeBlock
<<2,
980 /* If nodes == 1, we are at the correct nodecontainer, else go to next nodecontainer */
981 if (AROS_BE2LONG(volume
->blockbuffer
[4]) == 1)
983 /* read entry from position: be_node + sizeof(fsObjectNode) * (objectnode - be_nodenumber) */
984 tmpBytePtr
= (UBYTE
*)volume
->blockbuffer
;
985 ULONG index
= 20 + 10 * (objectnode
- AROS_BE2LONG(volume
->blockbuffer
[3]));
986 block_sfsobjectcontainer
= AROS_BE2LONG(((ULONG
*)(tmpBytePtr
+ index
))[0]);
987 D(bug("[install] collectBlockListSFS: leaf found in nextblock %ld, sfsobjectcontainer block is %ld \n",
988 nextblock
, block_sfsobjectcontainer
));
993 UWORD containerentry
=
994 (objectnode
- AROS_BE2LONG(volume
->blockbuffer
[3]))/AROS_BE2LONG(volume
->blockbuffer
[4]);
995 nextblock
= AROS_BE2LONG(volume
->blockbuffer
[containerentry
+ 5]) >> 4; /* 9-5 (2^9 = 512) */;
996 D(bug("[install] collectBlockListSFS: check next block %ld\n", nextblock
));
1000 if (block_sfsobjectcontainer
== 0)
1002 D(bug("[install] collectBlockListSFS: SFSObjectContainer not found\n"));
1003 Printf("SFSObjectContainer not found\n");
1009 /* Find the SFSObject in SFSObjectContainer for given objectnode */
1011 while((block_sfsobjectcontainer
!= 0) && (first_block
== 0))
1013 /* Read next SFS container block */
1014 retval
= _readwriteBlock(volume
, block_sfsobjectcontainer
, volume
->blockbuffer
, volume
->SizeBlock
<<2,
1019 D(bug("[install] collectBlockListSFS: ERROR reading block (error: %ld)\n", retval
));
1020 Printf("ReadError %lu\n", (long)retval
);
1024 /* Iterate over SFS objects and match the objectnode */
1026 * The first offset comes from :
1027 * sizeof(sfsblockheader) = uint32 + uint32 + uint32 (field of sfsobjectcontainer)
1028 * parent, next, previous = uint32 + uint32 + uint32 (fields of sfsobjectcontainers)
1030 tmpBytePtr
= ((UBYTE
*)volume
->blockbuffer
) + 12 + 12; /* tmpBytePtr points to first object in container */
1032 while (AROS_BE2LONG(((ULONG
*)(tmpBytePtr
+ 4))[0]) > 0) /* check on the objectnode field */
1035 /* Compare objectnode */
1036 if (AROS_BE2LONG(((ULONG
*)(tmpBytePtr
+ 4))[0]) == objectnode
)
1039 first_block
= AROS_BE2LONG(((ULONG
*)(tmpBytePtr
+ 12))[0]); /* data */
1040 D(bug("[install] collectBlockListSFS: first block is %ld\n", first_block
));
1044 /* Move to next object */
1045 /* Find end of name and end of comment */
1046 tmpBytePtr
+= 25; /* Point to name */
1048 for (i
= 2; i
> 0; tmpBytePtr
++, count
++)
1049 if (*tmpBytePtr
== '\0')
1052 /* Correction for aligment */
1053 if ((count
& 0x01) == 0 )
1057 /* Move to next sfs object container block */
1058 block_sfsobjectcontainer
= AROS_BE2LONG(volume
->blockbuffer
[4]); /* next field */
1062 if (first_block
== 0)
1064 D(bug("[install] collectBlockListSFS: First block not found\n"));
1065 Printf("First block not found\n");
1071 /* First file block found. Find all blocks of file */
1072 searchedblock
= first_block
;
1077 nextblock
= block_extentbnoderoot
;
1078 UBYTE
* BNodePtr
= NULL
;
1082 /* Find the extentbnode for this block */
1084 D(bug("[install] collectBlockListSFS: searching in nextblock %d for extentbnode for block %ld\n",
1085 nextblock
, searchedblock
));
1087 UBYTE
* BTreeContainerPtr
= NULL
;
1090 _readwriteBlock(volume
, nextblock
, volume
->blockbuffer
, volume
->SizeBlock
<<2,
1093 BTreeContainerPtr
= (UBYTE
*)(volume
->blockbuffer
+ 3); /* Starts right after the header */
1095 D(bug("[install] collectBlockListSFS: tree container nodecount: %d\n",
1096 AROS_BE2WORD(((UWORD
*)BTreeContainerPtr
)[0])));
1098 for (i
= AROS_BE2WORD(((UWORD
*)BTreeContainerPtr
)[0]) - 1; i
>=0; i
--) /* Start from last element */
1100 /* Read the BNode */
1101 tmpBytePtr
= BTreeContainerPtr
+ 4 + i
* BTreeContainerPtr
[3];
1103 if (AROS_BE2LONG(((ULONG
*)(tmpBytePtr
))[0]) <= searchedblock
) /* Check on the key field */
1105 BNodePtr
= tmpBytePtr
;
1110 /* Fail if BNodePtr still NULL */
1111 if (BNodePtr
== NULL
)
1113 D(bug("[install] collectBlockListSFS: Failed to travers extentbnode tree.\n"));
1114 Printf("Failed to travers extentbnode tree.\n");
1118 /* If we are at the leaf, stop */
1119 if (BTreeContainerPtr
[2])
1122 /* Else search further */
1123 nextblock
= AROS_BE2LONG(((ULONG
*)(BNodePtr
))[1]); /* data / next field */
1126 /* Found. Add BlockList entry */
1127 D(bug("[install] collectBlockListSFS: extentbnode for block %ld found. Block count: %d\n",
1128 searchedblock
, AROS_BE2WORD(((UWORD
*)(BNodePtr
+ 12))[0])));
1130 /* Add blocklist entry */
1133 /* Check if we still have spece left to add data to BlockList */
1134 if ((blk_count
-1) <= -BLCKLIST_ELEMENTS
)
1136 D(bug("[install] collectBlockListSFS: ERROR: out of block space\n"));
1137 Printf("There is no more space to save blocklist in core.img\n");
1141 blocklist
[blk_count
].sector_lo
= searchedblock
;
1142 blocklist
[blk_count
].sector_hi
= 0;
1143 blocklist
[blk_count
].count
= AROS_BE2WORD(((UWORD
*)(BNodePtr
+ 12))[0]);
1145 /* Handling of special situations */
1146 if (searchedblock
== first_block
)
1148 /* Writting first pack of blocks. Pointer needs to point to second file block */
1149 blocklist
[blk_count
].sector_lo
++;
1150 blocklist
[blk_count
].count
--;
1151 if (blocklist
[blk_count
].count
== 0)
1153 /* This means that the first pack of blocks contained only one block - first block */
1154 /* Since the first blocklist needs to start at second file block, 'reset' the blk_count */
1155 /* so that next iteration will overwrite the current results */
1160 /* Are there more blocks to read? */
1161 if (AROS_BE2LONG(((ULONG
*)(BNodePtr
))[1]) == 0)
1163 D(bug("[install] collectBlockListSFS: All core.img blocks found!\n"));
1167 searchedblock
= AROS_BE2LONG(((ULONG
*)(BNodePtr
))[1]); /* data / next field */
1171 /* Correct blocks for volume start */
1173 /* Blocks in blocklist are relative to the first sector of the HD (not partition) */
1175 for (count
=-1;count
>=blk_count
;count
--)
1177 blocklist
[count
].sector_lo
+= volume
->startblock
;
1178 blocklist
[count
].seg_adr
= 0x820 + (i
*32);
1179 i
+= blocklist
[count
].count
;
1180 D(bug("[install] collectBlockListFFS: correcting block %d for partition start\n", count
));
1181 D(bug("[install] collectBlockListFFS: sector : %ld seg_adr : %x\n",
1182 blocklist
[count
].sector_lo
, blocklist
[count
].seg_adr
));
1185 first_block
+= volume
->startblock
;
1190 /* Flushes the cache on the volume containing the specified path. */
1191 VOID
flushFS(CONST_STRPTR path
)
1196 for (i
= 0; path
[i
] != ':'; i
++)
1197 devname
[i
] = path
[i
];
1201 /* Try to flush 10 times. 5 seconds total */
1203 /* Failsafe in case first Inhibit fails in some way (was needed
1204 * for SFS because non flushed data was failing Inhibit) */
1206 for (i
= 0; i
< 10; i
++)
1208 if (Inhibit(devname
, DOSTRUE
))
1210 Inhibit(devname
, DOSFALSE
);
1218 BOOL
writeCoreIMG(BPTR fh
, UBYTE
*buffer
, struct Volume
*volume
)
1220 BOOL retval
= FALSE
;
1223 D(bug("[install] writeCoreIMG(%x)\n", volume
));
1225 if (Seek(fh
, 0, OFFSET_BEGINNING
) != -1)
1227 D(bug("[install] writeCoreIMG - write first block\n"));
1229 /* write back first block */
1230 if (Write(fh
, buffer
, 512) == 512)
1234 /* read second core.img block */
1235 if (Read(fh
, buffer
, 512) == 512)
1237 /* set partition number where core.img is on */
1238 /* FIXME: set RDB part number of DH? */
1239 UBYTE
*install_boot_device
=
1240 buffer
+ GRUB_DECOMPRESSOR_I386_PC_BOOT_DEVICE
;
1242 D(bug("[install] set dos part = %d\n", volume
->partnum
));
1244 install_boot_device
[0] = 0;
1245 install_boot_device
[1] = 0;
1246 install_boot_device
[2] = volume
->partnum
;
1248 /* write second core.img block back */
1249 if (Seek(fh
, -512, OFFSET_CURRENT
) != -1)
1251 if (Write(fh
, buffer
, 512) == 512)
1256 Printf("Write Error\n");
1259 Printf("Seek Error\n");
1262 Printf("Read Error\n");
1265 Printf("Write Error\n");
1270 Printf("Seek Error\n");
1271 PrintFault(error
, NULL
);
1276 ULONG
updateCoreIMG(CONST_STRPTR grubpath
, /* path of grub dir */
1277 struct Volume
*volume
, /* volume core.img is on */
1278 ULONG
*buffer
/* a buffer of at least 512 bytes */)
1281 struct FileInfoBlock fib
;
1283 TEXT coreimgpath
[256];
1285 D(bug("[install] updateCoreIMG(%x)\n", volume
));
1287 AddPart(coreimgpath
, grubpath
, 256);
1288 AddPart(coreimgpath
, CORE_IMG_FILE_NAME
, 256);
1289 fh
= Open(coreimgpath
, MODE_OLDFILE
);
1292 if (ExamineFH(fh
, &fib
))
1294 if (Read(fh
, buffer
, 512) == 512)
1297 Get and store all blocks of core.img in first block of core.img.
1298 First block of core.img will be returned.
1299 List of BlockNode starts at 512 - sizeof(BlockNode). List grows downwards.
1300 buffer is ULONG, buffer[128] is one pointer after first element(upwards).
1301 collectBlockList assumes it receives one pointer after first element(upwards).
1304 if (volume
->dos_id
== ID_SFS_BE_DISK
)
1306 D(bug("[install] core.img on SFS file system\n"));
1307 block
= collectBlockListSFS
1308 (volume
, fib
.fib_DiskKey
, (struct BlockNode
*)&buffer
[128]);
1311 if ((volume
->dos_id
== ID_FFS_DISK
) || (volume
->dos_id
== ID_INTER_DOS_DISK
) ||
1312 (volume
->dos_id
== ID_INTER_FFS_DISK
) || (volume
->dos_id
== ID_FASTDIR_DOS_DISK
) ||
1313 (volume
->dos_id
== ID_FASTDIR_FFS_DISK
))
1315 D(bug("[install] core.img on FFS file system\n"));
1316 block
= collectBlockListFFS
1317 (volume
, fib
.fib_DiskKey
, (struct BlockNode
*)&buffer
[128]);
1322 D(bug("[install] core.img on unsupported file system\n"));
1323 Printf("Unsupported file system\n");
1326 D(bug("[install] core.img first block: %ld\n", block
));
1330 if (!writeCoreIMG(fh
, (UBYTE
*)buffer
, volume
))
1335 Printf("%s: Read Error\n", coreimgpath
);
1338 PrintFault(IoErr(), coreimgpath
);
1344 PrintFault(IoErr(), coreimgpath
);
1348 /* Installs boot.img to MBR and updates core.img */
1349 BOOL
installGrubFiles(struct Volume
*coreimgvol
, /* core.img volume */
1350 CONST_STRPTR grubpath
, /* path to grub files */
1351 ULONG unit
, /* unit core.img is on */
1352 struct Volume
*bootimgvol
) /* boot device for boot.img */
1354 BOOL retval
= FALSE
;
1355 TEXT bootimgpath
[256];
1358 D(bug("[install] installStageFiles(%x)\n", bootimgvol
));
1360 /* Flush GRUB volume's cache */
1363 block
= updateCoreIMG(grubpath
, coreimgvol
, bootimgvol
->blockbuffer
);
1367 AddPart(bootimgpath
, grubpath
, 256);
1368 AddPart(bootimgpath
, (CONST_STRPTR
) "i386-pc/boot.img", 256);
1369 if (writeBootIMG(bootimgpath
, bootimgvol
, coreimgvol
, block
, unit
))
1373 bug("failed %d\n", IoErr());
1378 int main(int argc
, char **argv
)
1380 struct RDArgs
*rdargs
;
1381 struct Volume
*grubvol
;
1382 struct Volume
*bbvol
;
1383 struct FileSysStartupMsg
*fssm
;
1384 int ret
= RETURN_OK
;
1386 D(bug("[install] main()\n"));
1388 rdargs
= ReadArgs(template, myargs
, NULL
);
1391 CONST_STRPTR bootDevice
= (CONST_STRPTR
) myargs
[0];
1392 LONG unit
= *(LONG
*) myargs
[1];
1393 LONG
*partnum
= (LONG
*) myargs
[2];
1394 CONST_STRPTR grubpath
= (CONST_STRPTR
) myargs
[3];
1396 D(bug("[install] FORCELBA = %d\n", myargs
[4]));
1398 Printf("FORCELBA ignored\n");
1402 Printf("PARTITIONNUMBER not supported yet\n");
1404 return RETURN_ERROR
;
1407 fssm
= getDiskFSSM(grubpath
);
1410 CONST_STRPTR grubDevice
= AROS_BSTR_ADDR(fssm
->fssm_Device
);
1412 if (!strcmp((const char *) grubDevice
, (const char *) bootDevice
))
1414 struct DosEnvec
*dosEnvec
;
1415 dosEnvec
= (struct DosEnvec
*) BADDR(fssm
->fssm_Environ
);
1417 grubvol
= getGrubStageVolume(grubDevice
, fssm
->fssm_Unit
,
1418 fssm
->fssm_Flags
, dosEnvec
);
1422 bbvol
= getBBVolume(bootDevice
, unit
, partnum
);
1425 if (!installGrubFiles(grubvol
, grubpath
,
1426 fssm
->fssm_Unit
, bbvol
))
1429 uninitVolume(bbvol
);
1433 D(bug("getBBVolume failed miserably\n"));
1437 uninitVolume(grubvol
);
1442 Printf("%s is not on device %s unit %ld\n",
1443 grubpath
, bootDevice
, (long)unit
);
1449 Printf("kernel path must begin with a device name\n");
1457 PrintFault(IoErr(), (STRPTR
) argv
[0]);