123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <byteswap.h>
- #include <errno.h>
- #include <assert.h>
- #include <ipxe/list.h>
- #include <ipxe/blockdev.h>
- #include <ipxe/io.h>
- #include <ipxe/open.h>
- #include <ipxe/uri.h>
- #include <ipxe/process.h>
- #include <ipxe/xfer.h>
- #include <ipxe/retry.h>
- #include <ipxe/timer.h>
- #include <ipxe/acpi.h>
- #include <ipxe/sanboot.h>
- #include <ipxe/device.h>
- #include <ipxe/pci.h>
- #include <ipxe/iso9660.h>
- #include <ipxe/eltorito.h>
- #include <realmode.h>
- #include <bios.h>
- #include <biosint.h>
- #include <bootsector.h>
- #include <int13.h>
-
-
-
-
- #define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
-
-
- struct int13_drive {
-
- struct refcnt refcnt;
-
- struct list_head list;
-
-
- struct uri *uri;
-
- struct interface block;
-
-
- unsigned int drive;
-
-
- unsigned int natural_drive;
-
-
- struct block_device_capacity capacity;
-
-
- unsigned int blksize_shift;
-
-
-
- unsigned int cylinders;
-
-
- unsigned int heads;
-
-
- unsigned int sectors_per_track;
-
-
- int is_cdrom;
-
- unsigned int boot_catalog;
-
-
- int block_rc;
-
- int last_status;
- };
-
-
- static struct segoff __text16 ( int13_vector );
- #define int13_vector __use_text16 ( int13_vector )
-
-
- extern void int13_wrapper ( void );
-
-
- static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = {
-
- .bytes_per_sector = 0x02,
-
- .sectors_per_track = 48,
- };
- #define int13_fdd_params __use_data16 ( int13_fdd_params )
-
-
- static LIST_HEAD ( int13s );
-
-
- static uint16_t equipment_word;
-
-
- static uint8_t __text16 ( num_fdds );
- #define num_fdds __use_text16 ( num_fdds )
-
-
- static uint8_t __text16 ( num_drives );
- #define num_drives __use_text16 ( num_drives )
-
-
- static inline size_t int13_blksize ( struct int13_drive *int13 ) {
- return ( int13->capacity.blksize << int13->blksize_shift );
- }
-
-
- static inline uint64_t int13_capacity ( struct int13_drive *int13 ) {
- return ( int13->capacity.blocks >> int13->blksize_shift );
- }
-
-
- static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) {
- uint64_t capacity = int13_capacity ( int13 );
- return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
- }
-
-
- static inline int int13_is_fdd ( struct int13_drive *int13 ) {
- return ( ! ( int13->drive & 0x80 ) );
- }
-
-
- struct int13_command {
-
- int rc;
-
- struct int13_drive *int13;
-
- struct interface block;
-
- struct retry_timer timer;
- };
-
-
- static void int13_command_capacity ( struct int13_command *command,
- struct block_device_capacity *capacity ) {
- memcpy ( &command->int13->capacity, capacity,
- sizeof ( command->int13->capacity ) );
- }
-
-
- static void int13_command_close ( struct int13_command *command, int rc ) {
- intf_restart ( &command->block, rc );
- stop_timer ( &command->timer );
- command->rc = rc;
- }
-
-
- static void int13_command_expired ( struct retry_timer *timer,
- int over __unused ) {
- struct int13_command *command =
- container_of ( timer, struct int13_command, timer );
-
- int13_command_close ( command, -ETIMEDOUT );
- }
-
-
- static struct interface_operation int13_command_op[] = {
- INTF_OP ( intf_close, struct int13_command *, int13_command_close ),
- INTF_OP ( block_capacity, struct int13_command *,
- int13_command_capacity ),
- };
-
-
- static struct interface_descriptor int13_command_desc =
- INTF_DESC ( struct int13_command, block, int13_command_op );
-
-
- static int int13_reopen_block ( struct int13_drive *int13 ) {
- int rc;
-
-
- intf_restart ( &int13->block, -ECONNRESET );
-
-
- if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not reopen block "
- "device: %s\n", int13->drive, strerror ( rc ) );
- int13->block_rc = rc;
- return rc;
- }
-
-
- int13->block_rc = 0;
-
- return 0;
- }
-
-
- static int int13_command_start ( struct int13_command *command,
- struct int13_drive *int13 ) {
- int rc;
-
-
- assert ( command->int13 == NULL );
- assert ( ! timer_running ( &command->timer ) );
-
-
- if ( ( int13->block_rc != 0 ) &&
- ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
- return rc;
-
-
- command->rc = -EINPROGRESS;
- command->int13 = int13;
- start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT );
-
-
- while ( ( command->rc == -EINPROGRESS ) &&
- ( xfer_window ( &int13->block ) == 0 ) ) {
- step();
- }
-
- return ( ( command->rc == -EINPROGRESS ) ?
- int13->block_rc : command->rc );
- }
-
-
- static int int13_command_wait ( struct int13_command *command ) {
-
-
- assert ( timer_running ( &command->timer ) );
-
-
- while ( command->rc == -EINPROGRESS )
- step();
-
- assert ( ! timer_running ( &command->timer ) );
- return command->rc;
- }
-
-
- static void int13_command_stop ( struct int13_command *command ) {
- stop_timer ( &command->timer );
- command->int13 = NULL;
- }
-
-
- static struct int13_command int13_command = {
- .block = INTF_INIT ( int13_command_desc ),
- .timer = TIMER_INIT ( int13_command_expired ),
- };
-
-
- static int int13_rw ( struct int13_drive *int13, uint64_t lba,
- unsigned int count, userptr_t buffer,
- int ( * block_rw ) ( struct interface *control,
- struct interface *data,
- uint64_t lba, unsigned int count,
- userptr_t buffer, size_t len ) ) {
- struct int13_command *command = &int13_command;
- unsigned int frag_count;
- size_t frag_len;
- int rc;
-
-
- lba <<= int13->blksize_shift;
- count <<= int13->blksize_shift;
-
- while ( count ) {
-
-
- frag_count = count;
- if ( frag_count > int13->capacity.max_count )
- frag_count = int13->capacity.max_count;
- frag_len = ( int13->capacity.blksize * frag_count );
-
-
- if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
- ( ( rc = block_rw ( &int13->block, &command->block, lba,
- frag_count, buffer,
- frag_len ) ) != 0 ) ||
- ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
- int13_command_stop ( command );
- return rc;
- }
- int13_command_stop ( command );
-
-
- lba += frag_count;
- count -= frag_count;
- buffer = userptr_add ( buffer, frag_len );
- }
-
- return 0;
- }
-
-
- static int int13_read_capacity ( struct int13_drive *int13 ) {
- struct int13_command *command = &int13_command;
- int rc;
-
-
- if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
- ( ( rc = block_read_capacity ( &int13->block,
- &command->block ) ) != 0 ) ||
- ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
- int13_command_stop ( command );
- return rc;
- }
-
- int13_command_stop ( command );
- return 0;
- }
-
-
- static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) {
- static const struct iso9660_primary_descriptor_fixed primary_check = {
- .type = ISO9660_TYPE_PRIMARY,
- .id = ISO9660_ID,
- };
- struct iso9660_primary_descriptor *primary = scratch;
- static const struct eltorito_descriptor_fixed boot_check = {
- .type = ISO9660_TYPE_BOOT,
- .id = ISO9660_ID,
- .version = 1,
- .system_id = "EL TORITO SPECIFICATION",
- };
- struct eltorito_descriptor *boot = scratch;
- unsigned int blksize;
- unsigned int blksize_shift;
- int rc;
-
-
- blksize = int13_blksize ( int13 );
- blksize_shift = 0;
- while ( blksize < ISO9660_BLKSIZE ) {
- blksize <<= 1;
- blksize_shift++;
- }
- if ( blksize > ISO9660_BLKSIZE ) {
-
- return 0;
- }
-
-
- if ( ( rc = int13_rw ( int13,
- ( ISO9660_PRIMARY_LBA << blksize_shift ), 1,
- virt_to_user ( primary ), block_read ) ) != 0 ){
- DBGC ( int13, "INT13 drive %02x could not read ISO9660 "
- "primary volume descriptor: %s\n",
- int13->drive, strerror ( rc ) );
- return rc;
- }
-
-
- if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 )
- return 0;
- DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; "
- "treating as CD-ROM\n", int13->drive );
- int13->is_cdrom = 1;
-
-
- if ( ( rc = int13_rw ( int13,
- ( ELTORITO_LBA << blksize_shift ), 1,
- virt_to_user ( boot ), block_read ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not read El Torito boot "
- "record volume descriptor: %s\n",
- int13->drive, strerror ( rc ) );
- return rc;
- }
-
-
- if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) {
- int13->boot_catalog = boot->sector;
- DBGC ( int13, "INT13 drive %02x has an El Torito boot catalog "
- "at LBA %08x\n", int13->drive, int13->boot_catalog );
- } else {
- DBGC ( int13, "INT13 drive %02x has no El Torito boot "
- "catalog\n", int13->drive );
- }
-
-
- int13->blksize_shift += blksize_shift;
-
- return 0;
- }
-
-
- static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch,
- unsigned int *heads,
- unsigned int *sectors ) {
- struct master_boot_record *mbr = scratch;
- struct partition_table_entry *partition;
- unsigned int i;
- int rc;
-
-
- *heads = 255;
- *sectors = 63;
-
-
- if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( mbr ),
- block_read ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not read "
- "partition table to guess geometry: %s\n",
- int13->drive, strerror ( rc ) );
- return rc;
- }
- DBGC2 ( int13, "INT13 drive %02x has MBR:\n", int13->drive );
- DBGC2_HDA ( int13, 0, mbr, sizeof ( *mbr ) );
- DBGC ( int13, "INT13 drive %02x has signature %08x\n",
- int13->drive, mbr->signature );
-
-
-
- for ( i = 0 ; i < 4 ; i++ ) {
- partition = &mbr->partitions[i];
- if ( ! partition->type )
- continue;
- *heads = ( PART_HEAD ( partition->chs_end ) + 1 );
- *sectors = PART_SECTOR ( partition->chs_end );
- DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based "
- "on partition %d\n",
- int13->drive, *heads, *sectors, ( i + 1 ) );
- }
-
- return 0;
- }
-
-
- static const struct int13_fdd_geometry int13_fdd_geometries[] = {
- INT13_FDD_GEOMETRY ( 40, 1, 8 ),
- INT13_FDD_GEOMETRY ( 40, 1, 9 ),
- INT13_FDD_GEOMETRY ( 40, 2, 8 ),
- INT13_FDD_GEOMETRY ( 40, 1, 9 ),
- INT13_FDD_GEOMETRY ( 80, 2, 8 ),
- INT13_FDD_GEOMETRY ( 80, 2, 9 ),
- INT13_FDD_GEOMETRY ( 80, 2, 15 ),
- INT13_FDD_GEOMETRY ( 80, 2, 18 ),
- INT13_FDD_GEOMETRY ( 80, 2, 20 ),
- INT13_FDD_GEOMETRY ( 80, 2, 21 ),
- INT13_FDD_GEOMETRY ( 82, 2, 21 ),
- INT13_FDD_GEOMETRY ( 83, 2, 21 ),
- INT13_FDD_GEOMETRY ( 80, 2, 22 ),
- INT13_FDD_GEOMETRY ( 80, 2, 23 ),
- INT13_FDD_GEOMETRY ( 80, 2, 24 ),
- INT13_FDD_GEOMETRY ( 80, 2, 36 ),
- INT13_FDD_GEOMETRY ( 80, 2, 39 ),
- INT13_FDD_GEOMETRY ( 80, 2, 40 ),
- INT13_FDD_GEOMETRY ( 80, 2, 44 ),
- INT13_FDD_GEOMETRY ( 80, 2, 48 ),
- };
-
-
- static int int13_guess_geometry_fdd ( struct int13_drive *int13,
- unsigned int *heads,
- unsigned int *sectors ) {
- unsigned int blocks = int13_capacity ( int13 );
- const struct int13_fdd_geometry *geometry;
- unsigned int cylinders;
- unsigned int i;
-
-
- for ( i = 0 ; i < ( sizeof ( int13_fdd_geometries ) /
- sizeof ( int13_fdd_geometries[0] ) ) ; i++ ) {
- geometry = &int13_fdd_geometries[i];
- cylinders = INT13_FDD_CYLINDERS ( geometry );
- *heads = INT13_FDD_HEADS ( geometry );
- *sectors = INT13_FDD_SECTORS ( geometry );
- if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) {
- DBGC ( int13, "INT13 drive %02x guessing C/H/S "
- "%d/%d/%d based on size %dK\n", int13->drive,
- cylinders, *heads, *sectors, ( blocks / 2 ) );
- return 0;
- }
- }
-
-
-
- *heads = 2;
- *sectors = 18;
- DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size "
- "%dK\n", int13->drive, *heads, *sectors, ( blocks / 2 ) );
- return 0;
- }
-
-
- static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) {
- unsigned int guessed_heads;
- unsigned int guessed_sectors;
- unsigned int blocks;
- unsigned int blocks_per_cyl;
- int rc;
-
-
- if ( int13_blksize ( int13 ) != INT13_BLKSIZE )
- return 0;
-
-
- if ( int13_is_fdd ( int13 ) ) {
- if ( ( rc = int13_guess_geometry_fdd ( int13, &guessed_heads,
- &guessed_sectors )) != 0)
- return rc;
- } else {
- if ( ( rc = int13_guess_geometry_hdd ( int13, scratch,
- &guessed_heads,
- &guessed_sectors )) != 0)
- return rc;
- }
-
-
- if ( ! int13->heads )
- int13->heads = guessed_heads;
- if ( ! int13->sectors_per_track )
- int13->sectors_per_track = guessed_sectors;
- if ( ! int13->cylinders ) {
-
- blocks = int13_capacity32 ( int13 );
- blocks_per_cyl = ( int13->heads * int13->sectors_per_track );
- assert ( blocks_per_cyl != 0 );
- int13->cylinders = ( blocks / blocks_per_cyl );
- if ( int13->cylinders > 1024 )
- int13->cylinders = 1024;
- }
-
- return 0;
- }
-
-
- static void int13_sync_num_drives ( void ) {
- struct int13_drive *int13;
- uint8_t *counter;
- uint8_t max_drive;
- uint8_t required;
-
-
- get_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
- get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
- num_fdds = ( ( equipment_word & 0x0001 ) ?
- ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 );
-
-
- list_for_each_entry ( int13, &int13s, list ) {
- counter = ( int13_is_fdd ( int13 ) ? &num_fdds : &num_drives );
- max_drive = int13->drive;
- if ( max_drive < int13->natural_drive )
- max_drive = int13->natural_drive;
- required = ( ( max_drive & 0x7f ) + 1 );
- if ( *counter < required ) {
- *counter = required;
- DBGC ( int13, "INT13 drive %02x added to drive count: "
- "%d HDDs, %d FDDs\n",
- int13->drive, num_drives, num_fdds );
- }
- }
-
-
- equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 );
- if ( num_fdds ) {
- equipment_word |= ( 0x0001 |
- ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) );
- }
- put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
- put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
- }
-
-
- static void int13_check_num_drives ( void ) {
- uint16_t check_equipment_word;
- uint8_t check_num_drives;
-
- get_real ( check_equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
- get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
- if ( ( check_equipment_word != equipment_word ) ||
- ( check_num_drives != num_drives ) ) {
- int13_sync_num_drives();
- }
- }
-
-
- static int int13_reset ( struct int13_drive *int13,
- struct i386_all_regs *ix86 __unused ) {
- int rc;
-
- DBGC2 ( int13, "Reset drive\n" );
-
-
- if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
- return -INT13_STATUS_RESET_FAILED;
-
-
- if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
- return -INT13_STATUS_RESET_FAILED;
-
- return 0;
- }
-
-
- static int int13_get_last_status ( struct int13_drive *int13,
- struct i386_all_regs *ix86 __unused ) {
- DBGC2 ( int13, "Get status of last operation\n" );
- return int13->last_status;
- }
-
-
- static int int13_rw_sectors ( struct int13_drive *int13,
- struct i386_all_regs *ix86,
- int ( * block_rw ) ( struct interface *control,
- struct interface *data,
- uint64_t lba,
- unsigned int count,
- userptr_t buffer,
- size_t len ) ) {
- unsigned int cylinder, head, sector;
- unsigned long lba;
- unsigned int count;
- userptr_t buffer;
- int rc;
-
-
- if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
- DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
- "for non-extended read/write\n",
- int13->drive, int13_blksize ( int13 ) );
- return -INT13_STATUS_INVALID;
- }
-
-
- cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
- head = ix86->regs.dh;
- sector = ( ix86->regs.cl & 0x3f );
- if ( ( cylinder >= int13->cylinders ) ||
- ( head >= int13->heads ) ||
- ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) {
- DBGC ( int13, "C/H/S %d/%d/%d out of range for geometry "
- "%d/%d/%d\n", cylinder, head, sector, int13->cylinders,
- int13->heads, int13->sectors_per_track );
- return -INT13_STATUS_INVALID;
- }
- lba = ( ( ( ( cylinder * int13->heads ) + head )
- * int13->sectors_per_track ) + sector - 1 );
- count = ix86->regs.al;
- buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
-
- DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n",
- cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx,
- count );
-
-
- if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x I/O failed: %s\n",
- int13->drive, strerror ( rc ) );
- return -INT13_STATUS_READ_ERROR;
- }
-
- return 0;
- }
-
-
- static int int13_read_sectors ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- DBGC2 ( int13, "Read: " );
- return int13_rw_sectors ( int13, ix86, block_read );
- }
-
-
- static int int13_write_sectors ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- DBGC2 ( int13, "Write: " );
- return int13_rw_sectors ( int13, ix86, block_write );
- }
-
-
- static int int13_get_parameters ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- unsigned int max_cylinder = int13->cylinders - 1;
- unsigned int max_head = int13->heads - 1;
- unsigned int max_sector = int13->sectors_per_track;
-
- DBGC2 ( int13, "Get drive parameters\n" );
-
-
- if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
- DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
- "for non-extended parameters\n",
- int13->drive, int13_blksize ( int13 ) );
- return -INT13_STATUS_INVALID;
- }
-
-
- ix86->regs.ch = ( max_cylinder & 0xff );
- ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
- ix86->regs.dh = max_head;
- ix86->regs.dl = ( int13_is_fdd ( int13 ) ? num_fdds : num_drives );
-
-
- if ( int13_is_fdd ( int13 ) ) {
- ix86->regs.bl = INT13_FDD_TYPE_1M44;
- ix86->segs.es = rm_ds;
- ix86->regs.di = __from_data16 ( &int13_fdd_params );
- }
-
- return 0;
- }
-
-
- static int int13_get_disk_type ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- uint32_t blocks;
-
- DBGC2 ( int13, "Get disk type\n" );
-
- if ( int13_is_fdd ( int13 ) ) {
- return INT13_DISK_TYPE_FDD;
- } else {
- blocks = int13_capacity32 ( int13 );
- ix86->regs.cx = ( blocks >> 16 );
- ix86->regs.dx = ( blocks & 0xffff );
- return INT13_DISK_TYPE_HDD;
- }
- }
-
-
- static int int13_extension_check ( struct int13_drive *int13 __unused,
- struct i386_all_regs *ix86 ) {
- if ( ix86->regs.bx == 0x55aa ) {
- DBGC2 ( int13, "INT13 extensions installation check\n" );
- ix86->regs.bx = 0xaa55;
- ix86->regs.cx = ( INT13_EXTENSION_LINEAR |
- INT13_EXTENSION_EDD |
- INT13_EXTENSION_64BIT );
- return INT13_EXTENSION_VER_3_0;
- } else {
- return -INT13_STATUS_INVALID;
- }
- }
-
-
- static int int13_extended_rw ( struct int13_drive *int13,
- struct i386_all_regs *ix86,
- int ( * block_rw ) ( struct interface *control,
- struct interface *data,
- uint64_t lba,
- unsigned int count,
- userptr_t buffer,
- size_t len ) ) {
- struct int13_disk_address addr;
- uint8_t bufsize;
- uint64_t lba;
- unsigned long count;
- userptr_t buffer;
- int rc;
-
-
-
- if ( int13_is_fdd ( int13 ) )
- return -INT13_STATUS_INVALID;
-
-
- get_real ( bufsize, ix86->segs.ds,
- ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
- if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
- DBGC2 ( int13, "<invalid buffer size %#02x\n>\n", bufsize );
- return -INT13_STATUS_INVALID;
- }
-
-
- memset ( &addr, 0, sizeof ( addr ) );
- copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
- lba = addr.lba;
- DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) );
- if ( ( addr.count == 0xff ) ||
- ( ( addr.buffer.segment == 0xffff ) &&
- ( addr.buffer.offset == 0xffff ) ) ) {
- buffer = phys_to_user ( addr.buffer_phys );
- DBGC2 ( int13, "%08llx",
- ( ( unsigned long long ) addr.buffer_phys ) );
- } else {
- buffer = real_to_user ( addr.buffer.segment,
- addr.buffer.offset );
- DBGC2 ( int13, "%04x:%04x", addr.buffer.segment,
- addr.buffer.offset );
- }
- if ( addr.count <= 0x7f ) {
- count = addr.count;
- } else if ( addr.count == 0xff ) {
- count = addr.long_count;
- } else {
- DBGC2 ( int13, " <invalid count %#02x>\n", addr.count );
- return -INT13_STATUS_INVALID;
- }
- DBGC2 ( int13, " (count %ld)\n", count );
-
-
- if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n",
- int13->drive, strerror ( rc ) );
-
- addr.count = 0;
- put_real ( addr.count, ix86->segs.ds,
- ( ix86->regs.si +
- offsetof ( typeof ( addr ), count ) ) );
- return -INT13_STATUS_READ_ERROR;
- }
-
- return 0;
- }
-
-
- static int int13_extended_read ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- DBGC2 ( int13, "Extended read: " );
- return int13_extended_rw ( int13, ix86, block_read );
- }
-
-
- static int int13_extended_write ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- DBGC2 ( int13, "Extended write: " );
- return int13_extended_rw ( int13, ix86, block_write );
- }
-
-
- static int int13_extended_verify ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- struct int13_disk_address addr;
- uint64_t lba;
- unsigned long count;
-
-
- if ( DBG_EXTRA ) {
- copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
- sizeof ( addr ));
- lba = addr.lba;
- count = addr.count;
- DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n",
- ( ( unsigned long long ) lba ), count );
- }
-
-
- return -INT13_STATUS_INVALID;
- }
-
-
- static int int13_extended_seek ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- struct int13_disk_address addr;
- uint64_t lba;
- unsigned long count;
-
-
- if ( DBG_EXTRA ) {
- copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
- sizeof ( addr ));
- lba = addr.lba;
- count = addr.count;
- DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n",
- ( ( unsigned long long ) lba ), count );
- }
-
-
- return 0;
- }
-
-
- static int int13_device_path_info ( struct int13_drive *int13,
- struct edd_device_path_information *dpi ) {
- struct device *device;
- struct device_description *desc;
- unsigned int i;
- uint8_t sum = 0;
- int rc;
-
-
- if ( ( int13->block_rc != 0 ) &&
- ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
- return rc;
-
-
- device = identify_device ( &int13->block );
- if ( ! device ) {
- DBGC ( int13, "INT13 drive %02x cannot identify hardware "
- "device\n", int13->drive );
- return -ENODEV;
- }
-
-
- desc = &device->desc;
- switch ( desc->bus_type ) {
- case BUS_TYPE_PCI:
- dpi->host_bus_type.type = EDD_BUS_TYPE_PCI;
- dpi->interface_path.pci.bus = PCI_BUS ( desc->location );
- dpi->interface_path.pci.slot = PCI_SLOT ( desc->location );
- dpi->interface_path.pci.function = PCI_FUNC ( desc->location );
- dpi->interface_path.pci.channel = 0xff;
- break;
- default:
- DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n",
- int13->drive, desc->bus_type );
- return -ENOTSUP;
- }
-
-
- if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type,
- &dpi->device_path ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x cannot identify block device: "
- "%s\n", int13->drive, strerror ( rc ) );
- return rc;
- }
-
-
- dpi->key = EDD_DEVICE_PATH_INFO_KEY;
- dpi->len = sizeof ( *dpi );
- for ( i = 0 ; i < sizeof ( *dpi ) ; i++ )
- sum += *( ( ( uint8_t * ) dpi ) + i );
- dpi->checksum -= sum;
-
- return 0;
- }
-
-
- static int int13_get_extended_parameters ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- struct int13_disk_parameters params;
- struct segoff address;
- size_t len = sizeof ( params );
- uint16_t bufsize;
- int rc;
-
-
- get_real ( bufsize, ix86->segs.ds,
- ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
-
- DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n",
- ix86->segs.ds, ix86->regs.si, bufsize );
-
-
- memset ( ¶ms, 0, sizeof ( params ) );
- params.flags = INT13_FL_DMA_TRANSPARENT;
- if ( ( int13->cylinders < 1024 ) &&
- ( int13_capacity ( int13 ) <= INT13_MAX_CHS_SECTORS ) ) {
- params.flags |= INT13_FL_CHS_VALID;
- }
- params.cylinders = int13->cylinders;
- params.heads = int13->heads;
- params.sectors_per_track = int13->sectors_per_track;
- params.sectors = int13_capacity ( int13 );
- params.sector_size = int13_blksize ( int13 );
- memset ( ¶ms.dpte, 0xff, sizeof ( params.dpte ) );
- if ( ( rc = int13_device_path_info ( int13, ¶ms.dpi ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not provide device "
- "path information: %s\n",
- int13->drive, strerror ( rc ) );
- len = offsetof ( typeof ( params ), dpi );
- }
-
-
-
- if ( bufsize < offsetof ( typeof ( params ), dpte ) )
- return -INT13_STATUS_INVALID;
- if ( bufsize < offsetof ( typeof ( params ), dpi ) ) {
- params.bufsize = offsetof ( typeof ( params ), dpte );
- } else {
- params.bufsize = offsetof ( typeof ( params ), dpi );
- }
-
- DBGC ( int13, "INT 13 drive %02x described using extended "
- "parameters:\n", int13->drive );
- address.segment = ix86->segs.ds;
- address.offset = ix86->regs.si;
- DBGC_HDA ( int13, address, ¶ms, len );
-
-
- if ( len > bufsize )
- len = bufsize;
- copy_to_real ( ix86->segs.ds, ix86->regs.si, ¶ms, len );
-
- return 0;
- }
-
-
- static int int13_cdrom_status_terminate ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- struct int13_cdrom_specification specification;
-
- DBGC2 ( int13, "Get CD-ROM emulation status to %04x:%04x%s\n",
- ix86->segs.ds, ix86->regs.si,
- ( ix86->regs.al ? "" : " and terminate" ) );
-
-
- if ( ! int13->is_cdrom ) {
- DBGC ( int13, "INT13 drive %02x is not a CD-ROM\n",
- int13->drive );
- return -INT13_STATUS_INVALID;
- }
-
-
- memset ( &specification, 0, sizeof ( specification ) );
- specification.size = sizeof ( specification );
- specification.drive = int13->drive;
-
-
- copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
- sizeof ( specification ) );
-
- return 0;
- }
-
-
-
- static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13,
- struct i386_all_regs *ix86 ) {
- struct int13_cdrom_boot_catalog_command command;
- int rc;
-
-
- copy_from_real ( &command, ix86->segs.ds, ix86->regs.si,
- sizeof ( command ) );
- DBGC2 ( int13, "Read CD-ROM boot catalog to %08x\n", command.buffer );
-
-
- if ( ! int13->boot_catalog ) {
- DBGC ( int13, "INT13 drive %02x has no boot catalog\n",
- int13->drive );
- return -INT13_STATUS_INVALID;
- }
-
-
- if ( ( rc = int13_rw ( int13, ( int13->boot_catalog + command.start ),
- command.count, phys_to_user ( command.buffer ),
- block_read ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not read boot catalog: "
- "%s\n", int13->drive, strerror ( rc ) );
- return -INT13_STATUS_READ_ERROR;
- }
-
- return 0;
- }
-
-
- static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
- int command = ix86->regs.ah;
- unsigned int bios_drive = ix86->regs.dl;
- struct int13_drive *int13;
- int status;
-
-
- int13_check_num_drives();
-
- list_for_each_entry ( int13, &int13s, list ) {
-
- if ( bios_drive != int13->drive ) {
-
- if ( bios_drive == int13->natural_drive ) {
- DBGC2 ( int13, "INT13,%02x (%02x) remapped to "
- "(%02x)\n", ix86->regs.ah,
- bios_drive, int13->drive );
- ix86->regs.dl = int13->drive;
- return;
- } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) &&
- ( command == INT13_CDROM_STATUS_TERMINATE )
- && int13->is_cdrom ) {
-
- } else {
- continue;
- }
- }
-
- DBGC2 ( int13, "INT13,%02x (%02x): ",
- ix86->regs.ah, bios_drive );
-
- switch ( command ) {
- case INT13_RESET:
- status = int13_reset ( int13, ix86 );
- break;
- case INT13_GET_LAST_STATUS:
- status = int13_get_last_status ( int13, ix86 );
- break;
- case INT13_READ_SECTORS:
- status = int13_read_sectors ( int13, ix86 );
- break;
- case INT13_WRITE_SECTORS:
- status = int13_write_sectors ( int13, ix86 );
- break;
- case INT13_GET_PARAMETERS:
- status = int13_get_parameters ( int13, ix86 );
- break;
- case INT13_GET_DISK_TYPE:
- status = int13_get_disk_type ( int13, ix86 );
- break;
- case INT13_EXTENSION_CHECK:
- status = int13_extension_check ( int13, ix86 );
- break;
- case INT13_EXTENDED_READ:
- status = int13_extended_read ( int13, ix86 );
- break;
- case INT13_EXTENDED_WRITE:
- status = int13_extended_write ( int13, ix86 );
- break;
- case INT13_EXTENDED_VERIFY:
- status = int13_extended_verify ( int13, ix86 );
- break;
- case INT13_EXTENDED_SEEK:
- status = int13_extended_seek ( int13, ix86 );
- break;
- case INT13_GET_EXTENDED_PARAMETERS:
- status = int13_get_extended_parameters ( int13, ix86 );
- break;
- case INT13_CDROM_STATUS_TERMINATE:
- status = int13_cdrom_status_terminate ( int13, ix86 );
- break;
- case INT13_CDROM_READ_BOOT_CATALOG:
- status = int13_cdrom_read_boot_catalog ( int13, ix86 );
- break;
- default:
- DBGC2 ( int13, "*** Unrecognised INT13 ***\n" );
- status = -INT13_STATUS_INVALID;
- break;
- }
-
-
- int13->last_status = status;
-
-
- if ( status < 0 ) {
- status = -status;
- DBGC ( int13, "INT13,%02x (%02x) failed with status "
- "%02x\n", ix86->regs.ah, int13->drive, status );
- } else {
- ix86->flags &= ~CF;
- }
- ix86->regs.ah = status;
-
-
- ix86->flags |= OF;
-
- return;
- }
- }
-
-
- static void int13_hook_vector ( void ) {
-
-
- __asm__ __volatile__ (
- TEXT16_CODE ( "\nint13_wrapper:\n\t"
-
- "pushw %%bp\n\t"
- "movw %%sp, %%bp\n\t"
- "pushw %%ax\n\t"
- "pushw %%dx\n\t"
-
- "orb $0, %%al\n\t"
- "stc\n\t"
- "pushl %0\n\t"
- "pushw %%cs\n\t"
- "call prot_call\n\t"
-
- "jo 1f\n\t"
- "pushfw\n\t"
- "lcall *%%cs:int13_vector\n\t"
- "\n1:\n\t"
-
- "pushfw\n\t"
- "popw 6(%%bp)\n\t"
-
-
- "cmpb $0x15, -1(%%bp)\n\t"
- "jne 2f\n\t"
- "testb $0x80, -4(%%bp)\n\t"
- "jnz 3f\n\t"
- "\n2:\n\t"
- "movb -4(%%bp), %%dl\n\t"
- "cmpb $0x08, -1(%%bp)\n\t"
- "jne 3f\n\t"
- "testb $0x80, %%dl\n\t"
- "movb %%cs:num_drives, %%dl\n\t"
- "jnz 3f\n\t"
- "movb %%cs:num_fdds, %%dl\n\t"
-
- "\n3:\n\t"
- "movw %%bp, %%sp\n\t"
- "popw %%bp\n\t"
- "iret\n\t" )
- : : "i" ( int13 ) );
-
- hook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper, &int13_vector );
- }
-
-
- static void int13_unhook_vector ( void ) {
- unhook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper,
- &int13_vector );
- }
-
-
- static size_t int13_block_window ( struct int13_drive *int13 __unused ) {
-
-
-
- return 0;
- }
-
-
- static void int13_block_close ( struct int13_drive *int13, int rc ) {
-
-
- if ( rc == 0 )
- rc = -ENOTCONN;
-
- DBGC ( int13, "INT13 drive %02x went away: %s\n",
- int13->drive, strerror ( rc ) );
-
-
- int13->block_rc = rc;
-
-
- intf_restart ( &int13->block, rc );
- }
-
-
- static struct interface_operation int13_block_op[] = {
- INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ),
- INTF_OP ( intf_close, struct int13_drive *, int13_block_close ),
- };
-
-
- static struct interface_descriptor int13_block_desc =
- INTF_DESC ( struct int13_drive, block, int13_block_op );
-
-
- static void int13_free ( struct refcnt *refcnt ) {
- struct int13_drive *int13 =
- container_of ( refcnt, struct int13_drive, refcnt );
-
- uri_put ( int13->uri );
- free ( int13 );
- }
-
-
- static int int13_hook ( struct uri *uri, unsigned int drive ) {
- struct int13_drive *int13;
- unsigned int natural_drive;
- void *scratch;
- int rc;
-
-
- int13_sync_num_drives();
- natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
-
-
- list_for_each_entry ( int13, &int13s, list ) {
- if ( int13->drive == drive ) {
- rc = -EADDRINUSE;
- goto err_in_use;
- }
- }
-
-
- int13 = zalloc ( sizeof ( *int13 ) );
- if ( ! int13 ) {
- rc = -ENOMEM;
- goto err_zalloc;
- }
- ref_init ( &int13->refcnt, int13_free );
- intf_init ( &int13->block, &int13_block_desc, &int13->refcnt );
- int13->uri = uri_get ( uri );
- int13->drive = drive;
- int13->natural_drive = natural_drive;
-
-
- if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
- goto err_reopen_block;
-
-
- if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
- goto err_read_capacity;
-
-
- scratch = malloc ( int13_blksize ( int13 ) );
- if ( ! scratch )
- goto err_alloc_scratch;
-
-
- if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 )
- goto err_parse_iso9660;
-
-
- if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 )
- goto err_guess_geometry;
-
- DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S "
- "geometry %d/%d/%d\n", int13->drive, int13->natural_drive,
- int13->cylinders, int13->heads, int13->sectors_per_track );
-
-
- if ( list_empty ( &int13s ) ) {
- int13_hook_vector();
- devices_get();
- }
-
-
- list_add ( &int13->list, &int13s );
-
-
- int13_sync_num_drives();
-
- free ( scratch );
- return 0;
-
- err_guess_geometry:
- err_parse_iso9660:
- free ( scratch );
- err_alloc_scratch:
- err_read_capacity:
- err_reopen_block:
- intf_shutdown ( &int13->block, rc );
- ref_put ( &int13->refcnt );
- err_zalloc:
- err_in_use:
- return rc;
- }
-
-
- static struct int13_drive * int13_find ( unsigned int drive ) {
- struct int13_drive *int13;
-
- list_for_each_entry ( int13, &int13s, list ) {
- if ( int13->drive == drive )
- return int13;
- }
- return NULL;
- }
-
-
- static void int13_unhook ( unsigned int drive ) {
- struct int13_drive *int13;
-
-
- int13 = int13_find ( drive );
- if ( ! int13 ) {
- DBG ( "INT13 cannot find emulated drive %02x\n", drive );
- return;
- }
-
-
- intf_shutdown ( &int13->block, 0 );
-
-
- list_del ( &int13->list );
-
-
-
-
- DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive );
-
-
- if ( list_empty ( &int13s ) ) {
- devices_put();
- int13_unhook_vector();
- }
-
-
- ref_put ( &int13->refcnt );
- }
-
-
- static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
- uint8_t status;
- int discard_b, discard_c, discard_d;
- uint16_t magic;
-
-
- address->segment = 0;
- address->offset = 0x7c00;
- __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
- "pushl %%ebx\n\t"
- "popw %%bx\n\t"
- "popw %%es\n\t"
- "stc\n\t"
- "sti\n\t"
- "int $0x13\n\t"
- "sti\n\t"
- "jc 1f\n\t"
- "xorw %%ax, %%ax\n\t"
- "\n1:\n\t"
- "popw %%es\n\t" )
- : "=a" ( status ), "=b" ( discard_b ),
- "=c" ( discard_c ), "=d" ( discard_d )
- : "a" ( 0x0201 ), "b" ( *address ),
- "c" ( 1 ), "d" ( drive ) );
- if ( status ) {
- DBG ( "INT13 drive %02x could not read MBR (status %02x)\n",
- drive, status );
- return -EIO;
- }
-
-
- get_real ( magic, address->segment,
- ( address->offset +
- offsetof ( struct master_boot_record, magic ) ) );
- if ( magic != INT13_MBR_MAGIC ) {
- DBG ( "INT13 drive %02x does not contain a valid MBR\n",
- drive );
- return -ENOEXEC;
- }
-
- return 0;
- }
-
-
- static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = {
- .size = sizeof ( struct int13_cdrom_boot_catalog_command ),
- .count = 1,
- .buffer = 0x7c00,
- .start = 0,
- };
- #define eltorito_cmd __use_data16 ( eltorito_cmd )
-
-
- static struct int13_disk_address __bss16 ( eltorito_address );
- #define eltorito_address __use_data16 ( eltorito_address )
-
-
- static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
- struct {
- struct eltorito_validation_entry valid;
- struct eltorito_boot_entry boot;
- } __attribute__ (( packed )) catalog;
- uint8_t status;
-
-
- __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
- "sti\n\t"
- "int $0x13\n\t"
- "sti\n\t"
- "jc 1f\n\t"
- "xorw %%ax, %%ax\n\t"
- "\n1:\n\t" )
- : "=a" ( status )
- : "a" ( 0x4d00 ), "d" ( drive ),
- "S" ( __from_data16 ( &eltorito_cmd ) ) );
- if ( status ) {
- DBG ( "INT13 drive %02x could not read El Torito boot catalog "
- "(status %02x)\n", drive, status );
- return -EIO;
- }
- copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
- sizeof ( catalog ) );
-
-
- if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) {
- DBG ( "INT13 drive %02x El Torito specifies unknown platform "
- "%02x\n", drive, catalog.valid.platform_id );
- return -ENOEXEC;
- }
- if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
- DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
- return -ENOEXEC;
- }
- if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) {
- DBG ( "INT13 drive %02x El Torito requires emulation "
- "type %02x\n", drive, catalog.boot.media_type );
- return -ENOTSUP;
- }
- DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n",
- drive, catalog.boot.start, catalog.boot.length );
- address->segment = ( catalog.boot.load_segment ?
- catalog.boot.load_segment : 0x7c0 );
- address->offset = 0;
- DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n",
- drive, address->segment, address->offset );
-
-
- eltorito_address.bufsize =
- offsetof ( typeof ( eltorito_address ), buffer_phys );
- eltorito_address.count = catalog.boot.length;
- eltorito_address.buffer = *address;
- eltorito_address.lba = catalog.boot.start;
- __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
- "sti\n\t"
- "int $0x13\n\t"
- "sti\n\t"
- "jc 1f\n\t"
- "xorw %%ax, %%ax\n\t"
- "\n1:\n\t" )
- : "=a" ( status )
- : "a" ( 0x4200 ), "d" ( drive ),
- "S" ( __from_data16 ( &eltorito_address ) ) );
- if ( status ) {
- DBG ( "INT13 drive %02x could not read El Torito boot image "
- "(status %02x)\n", drive, status );
- return -EIO;
- }
-
- return 0;
- }
-
-
- static int int13_boot ( unsigned int drive ) {
- struct memory_map memmap;
- struct segoff address;
- int rc;
-
-
- if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
- ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
- return rc;
-
-
-
- get_memmap ( &memmap );
-
-
- if ( ( rc = call_bootsector ( address.segment, address.offset,
- drive ) ) != 0 ) {
- DBG ( "INT13 drive %02x boot returned: %s\n",
- drive, strerror ( rc ) );
- return rc;
- }
-
- return -ECANCELED;
- }
-
-
- union xbft_table {
-
- struct acpi_description_header acpi;
-
- char pad[768];
- };
-
-
- static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
- #define xbftab __use_data16 ( xbftab )
-
-
- static int int13_describe ( unsigned int drive ) {
- struct int13_drive *int13;
- struct segoff xbft_address;
- int rc;
-
-
- int13 = int13_find ( drive );
- if ( ! int13 ) {
- DBG ( "INT13 cannot find emulated drive %02x\n", drive );
- return -ENODEV;
- }
-
-
- if ( ( int13->block_rc != 0 ) &&
- ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
- return rc;
-
-
- memset ( &xbftab, 0, sizeof ( xbftab ) );
-
-
- strncpy ( xbftab.acpi.oem_id, "FENSYS",
- sizeof ( xbftab.acpi.oem_id ) );
- strncpy ( xbftab.acpi.oem_table_id, "iPXE",
- sizeof ( xbftab.acpi.oem_table_id ) );
-
-
- if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi,
- sizeof ( xbftab ) ) ) != 0 ) {
- DBGC ( int13, "INT13 drive %02x could not create ACPI "
- "description: %s\n", int13->drive, strerror ( rc ) );
- return rc;
- }
-
-
- acpi_fix_checksum ( &xbftab.acpi );
- xbft_address.segment = rm_ds;
- xbft_address.offset = __from_data16 ( &xbftab );
- DBGC ( int13, "INT13 drive %02x described using boot firmware "
- "table:\n", int13->drive );
- DBGC_HDA ( int13, xbft_address, &xbftab,
- le32_to_cpu ( xbftab.acpi.length ) );
-
- return 0;
- }
-
- PROVIDE_SANBOOT_INLINE ( pcbios, san_default_drive );
- PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
- PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
- PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
- PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );
|