1 /* 2 * Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "bios.h" 8 9 #include <KernelExport.h> 10 #include <boot/platform.h> 11 #include <boot/partitions.h> 12 #include <boot/stdio.h> 13 #include <boot/stage2.h> 14 15 #include <string.h> 16 17 //#define TRACE_DEVICES 18 #ifdef TRACE_DEVICES 19 # define TRACE(x) dprintf x 20 #else 21 # define TRACE(x) ; 22 #endif 23 24 25 // exported from shell.S 26 extern uint8 gBootedFromImage; 27 extern uint8 gBootDriveID; 28 extern uint32 gBootPartitionOffset; 29 30 // int 0x13 definitions 31 #define BIOS_RESET_DISK_SYSTEM 0x0000 32 #define BIOS_READ 0x0200 33 #define BIOS_GET_DRIVE_PARAMETERS 0x0800 34 #define BIOS_IS_EXT_PRESENT 0x4100 35 #define BIOS_EXT_READ 0x4200 36 #define BIOS_EXT_WRITE 0x4300 37 #define BIOS_GET_EXT_DRIVE_PARAMETERS 0x4800 38 #define BIOS_BOOT_CD_GET_STATUS 0x4b01 39 40 struct real_addr { 41 uint16 offset; 42 uint16 segment; 43 }; 44 45 struct disk_address_packet { 46 uint8 size; 47 uint8 reserved; 48 uint16 number_of_blocks; 49 uint32 buffer; 50 uint64 lba; 51 uint64 flat_buffer; 52 }; 53 54 static const uint16 kParametersSizeVersion1 = 0x1a; 55 static const uint16 kParametersSizeVersion2 = 0x1e; 56 static const uint16 kParametersSizeVersion3 = 0x42; 57 58 static const uint16 kDevicePathSignature = 0xbedd; 59 60 struct drive_parameters { 61 uint16 parameters_size; 62 uint16 flags; 63 uint32 cylinders; 64 uint32 heads; 65 uint32 sectors_per_track; 66 uint64 sectors; 67 uint16 bytes_per_sector; 68 /* edd 2.0 */ 69 real_addr device_table; 70 /* edd 3.0 */ 71 uint16 device_path_signature; 72 uint8 device_path_size; 73 uint8 reserved1[3]; 74 char host_bus[4]; 75 char interface_type[8]; 76 union { 77 struct { 78 uint16 base_address; 79 } legacy; 80 struct { 81 uint8 bus; 82 uint8 slot; 83 uint8 function; 84 } pci; 85 uint8 reserved[8]; 86 } interface; 87 union { 88 struct { 89 uint8 slave; 90 } ata; 91 struct { 92 uint8 slave; 93 uint8 logical_unit; 94 } atapi; 95 struct { 96 uint8 logical_unit; 97 } scsi; 98 struct { 99 uint8 tbd; 100 } usb; 101 struct { 102 uint64 guid; 103 } firewire; 104 struct { 105 uint64 wwd; 106 } fibre; 107 } device; 108 uint8 reserved2; 109 uint8 checksum; 110 } _PACKED; 111 112 struct device_table { 113 uint16 base_address; 114 uint16 control_port_address; 115 uint8 _reserved1 : 4; 116 uint8 is_slave : 1; 117 uint8 _reserved2 : 1; 118 uint8 lba_enabled : 1; 119 } _PACKED; 120 121 struct specification_packet { 122 uint8 size; 123 uint8 media_type; 124 uint8 drive_number; 125 uint8 controller_index; 126 uint32 start_emulation; 127 uint16 device_specification; 128 uint8 _more_[9]; 129 } _PACKED; 130 131 class BIOSDrive : public Node { 132 public: 133 BIOSDrive(uint8 driveID); 134 virtual ~BIOSDrive(); 135 136 status_t InitCheck() const; 137 138 virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize); 139 virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize); 140 141 virtual off_t Size() const; 142 143 uint32 BlockSize() const { return fBlockSize; } 144 145 status_t FillIdentifier(); 146 147 bool HasParameters() const { return fHasParameters; } 148 const drive_parameters &Parameters() const { return fParameters; } 149 150 disk_identifier &Identifier() { return fIdentifier; } 151 uint8 DriveID() const { return fDriveID; } 152 153 protected: 154 uint8 fDriveID; 155 bool fLBA; 156 uint64 fSize; 157 uint32 fBlockSize; 158 bool fHasParameters; 159 drive_parameters fParameters; 160 disk_identifier fIdentifier; 161 }; 162 163 164 static bool sBlockDevicesAdded = false; 165 166 167 static void 168 check_cd_boot(BIOSDrive *drive) 169 { 170 gBootVolume.SetInt32(BOOT_METHOD, BOOT_METHOD_HARD_DISK); 171 172 if (drive->DriveID() != 0) 173 return; 174 175 struct bios_regs regs; 176 regs.eax = BIOS_BOOT_CD_GET_STATUS; 177 regs.edx = 0; 178 regs.esi = kDataSegmentScratch; 179 call_bios(0x13, ®s); 180 181 if ((regs.flags & CARRY_FLAG) != 0) 182 return; 183 184 // we obviously were booted from CD! 185 186 specification_packet *packet = (specification_packet *)kDataSegmentScratch; 187 if (packet->media_type != 0) 188 gBootVolume.SetInt32(BOOT_METHOD, BOOT_METHOD_CD); 189 190 #if 0 191 dprintf("got CD boot spec:\n"); 192 dprintf(" size: %#x\n", packet->size); 193 dprintf(" media type: %u\n", packet->media_type); 194 dprintf(" drive_number: %u\n", packet->drive_number); 195 dprintf(" controller index: %u\n", packet->controller_index); 196 dprintf(" start emulation: %lu\n", packet->start_emulation); 197 dprintf(" device_specification: %u\n", packet->device_specification); 198 #endif 199 } 200 201 202 static bool 203 are_extensions_available(uint8 drive) 204 { 205 struct bios_regs regs; 206 regs.eax = BIOS_IS_EXT_PRESENT; 207 regs.ebx = 0x55aa; 208 regs.edx = drive; 209 call_bios(0x13, ®s); 210 211 TRACE(("checking extensions: carry: %u; ebx: 0x%08lx; ecx: 0x%08lx\n", 212 regs.flags & CARRY_FLAG, regs.ebx, regs.ecx)); 213 return (regs.flags & CARRY_FLAG) == 0 && regs.ebx == 0xaa55 214 && (regs.ecx & 0x01 /* supports device access using packet */) != 0; 215 } 216 217 218 static status_t 219 get_ext_drive_parameters(uint8 drive, drive_parameters *targetParameters) 220 { 221 drive_parameters *parameter = (drive_parameters *)kDataSegmentScratch; 222 223 memset(parameter, 0, sizeof(drive_parameters)); 224 parameter->parameters_size = sizeof(drive_parameters); 225 226 struct bios_regs regs; 227 regs.eax = BIOS_GET_EXT_DRIVE_PARAMETERS; 228 regs.edx = drive; 229 regs.esi = (addr_t)parameter - kDataSegmentBase; 230 call_bios(0x13, ®s); 231 232 // filter out faulty BIOS return codes 233 if ((regs.flags & CARRY_FLAG) != 0 234 || parameter->sectors == 0) 235 return B_ERROR; 236 237 memcpy(targetParameters, parameter, sizeof(drive_parameters)); 238 return B_OK; 239 } 240 241 242 static status_t 243 get_drive_parameters(uint8 drive, drive_parameters *parameters) 244 { 245 struct bios_regs regs; 246 regs.eax = BIOS_GET_DRIVE_PARAMETERS; 247 regs.edx = drive; 248 regs.es = 0; 249 regs.edi = 0; // guard against faulty BIOS, see Ralf Brown's interrupt list 250 call_bios(0x13, ®s); 251 252 if ((regs.flags & CARRY_FLAG) != 0 || (regs.ecx & 0x3f) == 0) 253 return B_ERROR; 254 255 // fill drive_parameters structure with useful values 256 parameters->parameters_size = kParametersSizeVersion1; 257 parameters->flags = 0; 258 parameters->cylinders = (((regs.ecx & 0xc0) << 2) | ((regs.ecx >> 8) & 0xff)) + 1; 259 parameters->heads = ((regs.edx >> 8) & 0xff) + 1; 260 // heads and cylinders start counting from 0 261 parameters->sectors_per_track = regs.ecx & 0x3f; 262 parameters->sectors = parameters->cylinders * parameters->heads 263 * parameters->sectors_per_track; 264 parameters->bytes_per_sector = 512; 265 266 return B_OK; 267 } 268 269 270 static status_t 271 get_number_of_drives(uint8 *_count) 272 { 273 struct bios_regs regs; 274 regs.eax = BIOS_GET_DRIVE_PARAMETERS; 275 regs.edx = 0x80; 276 regs.es = 0; 277 regs.edi = 0; 278 call_bios(0x13, ®s); 279 280 if (regs.flags & CARRY_FLAG) 281 return B_ERROR; 282 283 *_count = regs.edx & 0xff; 284 return B_OK; 285 } 286 287 288 /** parse EDD 3.0 drive path information */ 289 290 static status_t 291 fill_disk_identifier_v3(disk_identifier &disk, const drive_parameters ¶meters) 292 { 293 if (parameters.parameters_size < kParametersSizeVersion3 294 || parameters.device_path_signature != kDevicePathSignature) 295 return B_BAD_TYPE; 296 297 // parse host bus 298 299 if (!strncmp(parameters.host_bus, "PCI", 3)) { 300 disk.bus_type = PCI_BUS; 301 302 disk.bus.pci.bus = parameters.interface.pci.bus; 303 disk.bus.pci.slot = parameters.interface.pci.slot; 304 disk.bus.pci.function = parameters.interface.pci.function; 305 } else if (!strncmp(parameters.host_bus, "ISA", 3)) { 306 disk.bus_type = LEGACY_BUS; 307 308 disk.bus.legacy.base_address = parameters.interface.legacy.base_address; 309 dprintf("legacy base address %x\n", disk.bus.legacy.base_address); 310 } else { 311 dprintf("unknown host bus \"%s\"\n", parameters.host_bus); 312 return B_BAD_DATA; 313 } 314 315 // parse interface 316 317 if (!strncmp(parameters.interface_type, "ATA", 3)) { 318 disk.device_type = ATA_DEVICE; 319 disk.device.ata.master = !parameters.device.ata.slave; 320 dprintf("ATA device, %s\n", disk.device.ata.master ? "master" : "slave"); 321 } else if (!strncmp(parameters.interface_type, "ATAPI", 3)) { 322 disk.device_type = ATAPI_DEVICE; 323 disk.device.atapi.master = !parameters.device.ata.slave; 324 disk.device.atapi.logical_unit = parameters.device.atapi.logical_unit; 325 } else if (!strncmp(parameters.interface_type, "SCSI", 3)) { 326 disk.device_type = SCSI_DEVICE; 327 disk.device.scsi.logical_unit = parameters.device.scsi.logical_unit; 328 } else if (!strncmp(parameters.interface_type, "USB", 3)) { 329 disk.device_type = USB_DEVICE; 330 disk.device.usb.tbd = parameters.device.usb.tbd; 331 } else if (!strncmp(parameters.interface_type, "1394", 3)) { 332 disk.device_type = FIREWIRE_DEVICE; 333 disk.device.firewire.guid = parameters.device.firewire.guid; 334 } else if (!strncmp(parameters.interface_type, "FIBRE", 3)) { 335 disk.device_type = FIBRE_DEVICE; 336 disk.device.fibre.wwd = parameters.device.fibre.wwd; 337 } else { 338 dprintf("unknown interface type \"%s\"\n", parameters.interface_type); 339 return B_BAD_DATA; 340 } 341 342 return B_OK; 343 } 344 345 346 /** EDD 2.0 drive table information */ 347 348 static status_t 349 fill_disk_identifier_v2(disk_identifier &disk, const drive_parameters ¶meters) 350 { 351 if (parameters.device_table.segment == 0xffff 352 && parameters.device_table.offset == 0xffff) 353 return B_BAD_TYPE; 354 355 device_table *table = (device_table *)LINEAR_ADDRESS(parameters.device_table.segment, 356 parameters.device_table.offset); 357 358 disk.bus_type = LEGACY_BUS; 359 disk.bus.legacy.base_address = table->base_address; 360 361 disk.device_type = ATA_DEVICE; 362 disk.device.ata.master = !table->is_slave; 363 364 return B_OK; 365 } 366 367 368 static off_t 369 get_next_check_sum_offset(int32 index, off_t maxSize) 370 { 371 // The boot block often contains the disk superblock, and should be 372 // unique enough for most cases 373 if (index < 2) 374 return index * 512; 375 376 // Try some data in the first part of the drive 377 if (index < 4) 378 return (maxSize >> 10) + index * 2048; 379 380 // Some random value might do 381 return ((system_time() + index) % (maxSize >> 9)) * 512; 382 } 383 384 385 /** Computes a check sum for the specified block. 386 * The check sum is the sum of all data in that block interpreted as an 387 * array of uint32 values. 388 * Note, this must use the same method as the one used in kernel/fs/vfs_boot.cpp. 389 */ 390 391 static uint32 392 compute_check_sum(BIOSDrive *drive, off_t offset) 393 { 394 char buffer[512]; 395 ssize_t bytesRead = drive->ReadAt(NULL, offset, buffer, sizeof(buffer)); 396 if (bytesRead < B_OK) 397 return 0; 398 399 if (bytesRead < (ssize_t)sizeof(buffer)) 400 memset(buffer + bytesRead, 0, sizeof(buffer) - bytesRead); 401 402 uint32 *array = (uint32 *)buffer; 403 uint32 sum = 0; 404 405 for (uint32 i = 0; i < (bytesRead + sizeof(uint32) - 1) / sizeof(uint32); i++) { 406 sum += array[i]; 407 } 408 409 return sum; 410 } 411 412 413 static void 414 find_unique_check_sums(NodeList *devices) 415 { 416 NodeIterator iterator = devices->GetIterator(); 417 Node *device; 418 int32 index = 0; 419 off_t minSize = 0; 420 const int32 kMaxTries = 200; 421 422 while (index < kMaxTries) { 423 bool clash = false; 424 425 iterator.Rewind(); 426 427 while ((device = iterator.Next()) != NULL) { 428 BIOSDrive *drive = (BIOSDrive *)device; 429 #if 0 430 // there is no RTTI in the boot loader... 431 BIOSDrive *drive = dynamic_cast<BIOSDrive *>(device); 432 if (drive == NULL) 433 continue; 434 #endif 435 436 // TODO: currently, we assume that the BIOS provided us with unique 437 // disk identifiers... hopefully this is a good idea 438 if (drive->Identifier().device_type != UNKNOWN_DEVICE) 439 continue; 440 441 if (minSize == 0 || drive->Size() < minSize) 442 minSize = drive->Size(); 443 444 // check for clashes 445 446 NodeIterator compareIterator = devices->GetIterator(); 447 while ((device = compareIterator.Next()) != NULL) { 448 BIOSDrive *compareDrive = (BIOSDrive *)device; 449 450 if (compareDrive == drive 451 || compareDrive->Identifier().device_type != UNKNOWN_DEVICE) 452 continue; 453 454 // TODO: Until we can actually get and compare *all* fields of the disk 455 // identifier in the kernel, we cannot compare the whole structure (we also 456 // should be more careful zeroing the structure before we fill it). 457 #if 0 458 if (!memcmp(&drive->Identifier(), &compareDrive->Identifier(), 459 sizeof(disk_identifier))) { 460 clash = true; 461 break; 462 } 463 #else 464 const disk_identifier& ourId = drive->Identifier(); 465 const disk_identifier& otherId = compareDrive->Identifier(); 466 if (memcmp(&ourId.device.unknown.check_sums, 467 &otherId.device.unknown.check_sums, 468 sizeof(ourId.device.unknown.check_sums)) == 0) { 469 clash = true; 470 } 471 #endif 472 } 473 474 if (clash) 475 break; 476 } 477 478 if (!clash) { 479 // our work here is done. 480 return; 481 } 482 483 // add a new block to the check sums 484 485 off_t offset = get_next_check_sum_offset(index, minSize); 486 int32 i = index % NUM_DISK_CHECK_SUMS; 487 iterator.Rewind(); 488 489 while ((device = iterator.Next()) != NULL) { 490 BIOSDrive *drive = (BIOSDrive *)device; 491 492 disk_identifier& disk = drive->Identifier(); 493 disk.device.unknown.check_sums[i].offset = offset; 494 disk.device.unknown.check_sums[i].sum = compute_check_sum(drive, offset); 495 496 TRACE(("disk %x, offset %Ld, sum %lu\n", drive->DriveID(), offset, 497 disk.device.unknown.check_sums[i].sum)); 498 } 499 500 index++; 501 } 502 503 // If we get here, we couldn't find a way to differentiate all disks from each other. 504 // It's very likely that one disk is an exact copy of the other, so there is nothing 505 // we could do, anyway. 506 507 dprintf("Could not make BIOS drives unique! Might boot from the wrong disk...\n"); 508 } 509 510 511 static status_t 512 add_block_devices(NodeList *devicesList, bool identifierMissing) 513 { 514 if (sBlockDevicesAdded) 515 return B_OK; 516 517 uint8 driveCount; 518 if (get_number_of_drives(&driveCount) != B_OK) 519 return B_ERROR; 520 521 dprintf("number of drives: %d\n", driveCount); 522 523 for (int32 i = 0; i < driveCount; i++) { 524 uint8 driveID = i + 0x80; 525 if (driveID == gBootDriveID) 526 continue; 527 528 BIOSDrive *drive = new(nothrow) BIOSDrive(driveID); 529 if (drive->InitCheck() != B_OK) { 530 dprintf("could not add drive %u\n", driveID); 531 delete drive; 532 continue; 533 } 534 535 devicesList->Add(drive); 536 537 if (drive->FillIdentifier() != B_OK) 538 identifierMissing = true; 539 } 540 541 if (identifierMissing) { 542 // we cannot distinguish between all drives by identifier, we need 543 // compute checksums for them 544 find_unique_check_sums(devicesList); 545 } 546 547 sBlockDevicesAdded = true; 548 return B_OK; 549 } 550 551 552 // #pragma mark - 553 554 555 BIOSDrive::BIOSDrive(uint8 driveID) 556 : 557 fDriveID(driveID), 558 fSize(0) 559 { 560 TRACE(("drive ID %u\n", driveID)); 561 562 if (driveID < 0x80 || !are_extensions_available(driveID) 563 || get_ext_drive_parameters(driveID, &fParameters) != B_OK) { 564 // old style CHS support 565 566 if (get_drive_parameters(driveID, &fParameters) != B_OK) { 567 dprintf("getting drive parameters for: %u failed!\n", fDriveID); 568 return; 569 } 570 571 TRACE((" cylinders: %lu, heads: %lu, sectors: %lu, bytes_per_sector: %u\n", 572 fParameters.cylinders, fParameters.heads, fParameters.sectors_per_track, 573 fParameters.bytes_per_sector)); 574 TRACE((" total sectors: %Ld\n", fParameters.sectors)); 575 576 fBlockSize = 512; 577 fSize = fParameters.sectors * fBlockSize; 578 fLBA = false; 579 fHasParameters = false; 580 } else { 581 TRACE(("size: %x\n", fParameters.parameters_size)); 582 TRACE(("drive_path_signature: %x\n", fParameters.device_path_signature)); 583 TRACE(("host bus: \"%s\", interface: \"%s\"\n", fParameters.host_bus, 584 fParameters.interface_type)); 585 TRACE(("cylinders: %lu, heads: %lu, sectors: %lu, bytes_per_sector: %u\n", 586 fParameters.cylinders, fParameters.heads, fParameters.sectors_per_track, 587 fParameters.bytes_per_sector)); 588 TRACE(("total sectors: %Ld\n", fParameters.sectors)); 589 590 fBlockSize = fParameters.bytes_per_sector; 591 fSize = fParameters.sectors * fBlockSize; 592 fLBA = true; 593 fHasParameters = true; 594 } 595 } 596 597 598 BIOSDrive::~BIOSDrive() 599 { 600 } 601 602 603 status_t 604 BIOSDrive::InitCheck() const 605 { 606 return fSize > 0 ? B_OK : B_ERROR; 607 } 608 609 610 ssize_t 611 BIOSDrive::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) 612 { 613 uint32 offset = pos % fBlockSize; 614 pos /= fBlockSize; 615 616 uint32 blocksLeft = (bufferSize + offset + fBlockSize - 1) / fBlockSize; 617 int32 totalBytesRead = 0; 618 619 //TRACE(("BIOS reads %lu bytes from %Ld (offset = %lu), drive %u\n", 620 // blocksLeft * fBlockSize, pos * fBlockSize, offset, fDriveID)); 621 622 uint32 scratchSize = 24 * 1024 / fBlockSize; 623 // maximum value allowed by Phoenix BIOS is 0x7f 624 625 while (blocksLeft > 0) { 626 uint32 blocksRead = blocksLeft; 627 if (blocksRead > scratchSize) 628 blocksRead = scratchSize; 629 630 if (fLBA) { 631 struct disk_address_packet *packet = (disk_address_packet *)kDataSegmentScratch; 632 memset(packet, 0, sizeof(disk_address_packet)); 633 634 packet->size = sizeof(disk_address_packet); 635 packet->number_of_blocks = blocksRead; 636 packet->buffer = kExtraSegmentScratch; 637 packet->lba = pos; 638 639 struct bios_regs regs; 640 regs.eax = BIOS_EXT_READ; 641 regs.edx = fDriveID; 642 regs.esi = (addr_t)packet - kDataSegmentBase; 643 call_bios(0x13, ®s); 644 645 if (regs.flags & CARRY_FLAG) 646 goto chs_read; 647 } else { 648 chs_read: 649 // Old style CHS read routine 650 651 // We can only read up to 64 kB this way, but since scratchSize 652 // is actually lower than this value, we don't have to take care 653 // of this here. 654 655 uint32 sector = pos % fParameters.sectors_per_track + 1; 656 // sectors start countint at 1 (unlike head and cylinder) 657 uint32 head = pos / fParameters.sectors_per_track; 658 uint32 cylinder = head / fParameters.heads; 659 head %= fParameters.heads; 660 661 if (cylinder >= fParameters.cylinders) { 662 TRACE(("cylinder value %lu bigger than available %lu\n", 663 cylinder, fParameters.cylinders)); 664 return B_BAD_VALUE; 665 } 666 667 // try to read from the device more than once, just to make sure it'll work 668 struct bios_regs regs; 669 int32 tries = 3; 670 bool readWorked = false; 671 672 while (tries-- > 0) { 673 regs.eax = BIOS_READ | blocksRead; 674 regs.edx = fDriveID | (head << 8); 675 regs.ecx = sector | ((cylinder >> 2) & 0xc0) | ((cylinder & 0xff) << 8); 676 regs.es = 0; 677 regs.ebx = kExtraSegmentScratch; 678 call_bios(0x13, ®s); 679 680 if ((regs.flags & CARRY_FLAG) == 0) { 681 readWorked = true; 682 break; 683 } 684 685 TRACE(("read failed\n")); 686 687 if (tries < 2) { 688 // reset disk system 689 TRACE(("reset disk system\n")); 690 regs.eax = BIOS_RESET_DISK_SYSTEM; 691 regs.edx = fDriveID; 692 call_bios(0x13, ®s); 693 } 694 695 // wait a bit between the retries (1/20 sec) 696 spin(50000); 697 } 698 699 if (!readWorked) { 700 dprintf("reading %ld bytes from drive %u failed at %Ld\n", 701 blocksRead, fDriveID, pos); 702 return B_ERROR; 703 } 704 } 705 706 uint32 bytesRead = fBlockSize * blocksRead - offset; 707 // copy no more than bufferSize bytes 708 if (bytesRead > bufferSize) 709 bytesRead = bufferSize; 710 711 memcpy(buffer, (void *)(kExtraSegmentScratch + offset), bytesRead); 712 pos += blocksRead; 713 offset = 0; 714 blocksLeft -= blocksRead; 715 bufferSize -= bytesRead; 716 buffer = (void *)((addr_t)buffer + bytesRead); 717 totalBytesRead += bytesRead; 718 } 719 720 return totalBytesRead; 721 } 722 723 724 ssize_t 725 BIOSDrive::WriteAt(void* cookie, off_t pos, const void* buffer, 726 size_t bufferSize) 727 { 728 // we support only LBA addressing 729 if (!fLBA) { 730 dprintf("BIOSDrive::WriteAt(): CHS addressing not supported\n"); 731 return B_UNSUPPORTED; 732 } 733 734 // we support only block-aligned writes 735 if (pos % fBlockSize != 0 || bufferSize % fBlockSize != 0) { 736 dprintf("BIOSDrive::WriteAt(pos: %" B_PRIdOFF ", size: %" B_PRIuSIZE 737 "): Block-unaligned write not supported.\n", pos, bufferSize); 738 return B_UNSUPPORTED; 739 } 740 741 pos /= fBlockSize; 742 743 uint32 blocksLeft = bufferSize / fBlockSize; 744 int32 totalBytesWritten = 0; 745 746 uint32 scratchSize = 24 * 1024 / fBlockSize; 747 // maximum value allowed by Phoenix BIOS is 0x7f 748 749 while (blocksLeft > 0) { 750 uint32 blocksToWrite = blocksLeft; 751 if (blocksToWrite > scratchSize) 752 blocksToWrite = scratchSize; 753 754 uint32 bytesToWrite = blocksToWrite * fBlockSize; 755 756 memcpy((void*)kExtraSegmentScratch, buffer, bytesToWrite); 757 758 struct disk_address_packet* packet 759 = (disk_address_packet*)kDataSegmentScratch; 760 memset(packet, 0, sizeof(disk_address_packet)); 761 762 packet->size = sizeof(disk_address_packet); 763 packet->number_of_blocks = blocksToWrite; 764 packet->buffer = kExtraSegmentScratch; 765 packet->lba = pos; 766 767 struct bios_regs regs; 768 regs.eax = BIOS_EXT_WRITE; // al = 0x00 -- no write verify 769 regs.edx = fDriveID; 770 regs.esi = (addr_t)packet - kDataSegmentBase; 771 call_bios(0x13, ®s); 772 773 if (regs.flags & CARRY_FLAG) 774 return B_ERROR; 775 776 pos += blocksToWrite; 777 blocksLeft -= blocksToWrite; 778 bufferSize -= bytesToWrite; 779 buffer = (void*)((addr_t)buffer + bytesToWrite); 780 totalBytesWritten += bytesToWrite; 781 } 782 783 return totalBytesWritten; 784 } 785 786 787 off_t 788 BIOSDrive::Size() const 789 { 790 return fSize; 791 } 792 793 794 status_t 795 BIOSDrive::FillIdentifier() 796 { 797 if (HasParameters()) { 798 // try all drive_parameters versions, beginning from the most informative 799 800 #if 0 801 if (fill_disk_identifier_v3(fIdentifier, fParameters) == B_OK) 802 return B_OK; 803 804 if (fill_disk_identifier_v2(fIdentifier, fParameters) == B_OK) 805 return B_OK; 806 #else 807 // TODO: the above version is the correct one - it's currently 808 // disabled, as the kernel boot code only supports the 809 // UNKNOWN_BUS/UNKNOWN_DEVICE way to find the correct boot 810 // device. 811 if (fill_disk_identifier_v3(fIdentifier, fParameters) != B_OK) 812 fill_disk_identifier_v2(fIdentifier, fParameters); 813 814 #endif 815 816 // no interesting information, we have to fall back to the default 817 // unknown interface/device type identifier 818 } 819 820 fIdentifier.bus_type = UNKNOWN_BUS; 821 fIdentifier.device_type = UNKNOWN_DEVICE; 822 fIdentifier.device.unknown.size = Size(); 823 824 for (int32 i = 0; i < NUM_DISK_CHECK_SUMS; i++) { 825 fIdentifier.device.unknown.check_sums[i].offset = -1; 826 fIdentifier.device.unknown.check_sums[i].sum = 0; 827 } 828 829 return B_ERROR; 830 } 831 832 833 // #pragma mark - 834 835 836 status_t 837 platform_add_boot_device(struct stage2_args *args, NodeList *devicesList) 838 { 839 TRACE(("boot drive ID: %x\n", gBootDriveID)); 840 841 BIOSDrive *drive = new(nothrow) BIOSDrive(gBootDriveID); 842 if (drive->InitCheck() != B_OK) { 843 dprintf("no boot drive!\n"); 844 delete drive; 845 return B_ERROR; 846 } 847 848 devicesList->Add(drive); 849 850 if (drive->FillIdentifier() != B_OK) { 851 // We need to add all block devices to give the kernel the possibility 852 // to find the right boot volume 853 add_block_devices(devicesList, true); 854 } 855 856 TRACE(("boot drive size: %Ld bytes\n", drive->Size())); 857 gBootVolume.SetBool(BOOT_VOLUME_BOOTED_FROM_IMAGE, gBootedFromImage); 858 859 return B_OK; 860 } 861 862 863 status_t 864 platform_get_boot_partition(struct stage2_args *args, Node *bootDevice, 865 NodeList *list, boot::Partition **_partition) 866 { 867 BIOSDrive *drive = static_cast<BIOSDrive *>(bootDevice); 868 off_t offset = (off_t)gBootPartitionOffset * drive->BlockSize(); 869 870 dprintf("boot partition offset: %Ld\n", offset); 871 872 NodeIterator iterator = list->GetIterator(); 873 boot::Partition *partition = NULL; 874 while ((partition = (boot::Partition *)iterator.Next()) != NULL) { 875 TRACE(("partition offset = %Ld, size = %Ld\n", partition->offset, partition->size)); 876 // search for the partition that contains the partition 877 // offset as reported by the BFS boot block 878 if (offset >= partition->offset 879 && offset < partition->offset + partition->size) { 880 *_partition = partition; 881 return B_OK; 882 } 883 } 884 885 return B_ENTRY_NOT_FOUND; 886 } 887 888 889 status_t 890 platform_add_block_devices(stage2_args *args, NodeList *devicesList) 891 { 892 return add_block_devices(devicesList, false); 893 } 894 895 896 status_t 897 platform_register_boot_device(Node *device) 898 { 899 BIOSDrive *drive = (BIOSDrive *)device; 900 901 check_cd_boot(drive); 902 903 gBootVolume.SetInt64("boot drive number", drive->DriveID()); 904 gBootVolume.SetData(BOOT_VOLUME_DISK_IDENTIFIER, B_RAW_TYPE, 905 &drive->Identifier(), sizeof(disk_identifier)); 906 907 return B_OK; 908 } 909 910