-
Notifications
You must be signed in to change notification settings - Fork 642
/
int13.c
1636 lines (1461 loc) · 46.1 KB
/
int13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
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/blockdev.h>
#include <ipxe/io.h>
#include <ipxe/acpi.h>
#include <ipxe/sanboot.h>
#include <ipxe/device.h>
#include <ipxe/pci.h>
#include <ipxe/eltorito.h>
#include <realmode.h>
#include <bios.h>
#include <biosint.h>
#include <bootsector.h>
#include <int13.h>
/** @file
*
* INT 13 emulation
*
* This module provides a mechanism for exporting block devices via
* the BIOS INT 13 disk interrupt interface.
*
*/
/** INT 13 SAN device private data */
struct int13_data {
/** BIOS natural drive number (0x00-0xff)
*
* This is the drive number that would have been assigned by
* 'naturally' appending the drive to the end of the BIOS
* drive list.
*
* If the emulated drive replaces a preexisting drive, this is
* the drive number that the preexisting drive gets remapped
* to.
*/
unsigned int natural_drive;
/** Number of cylinders
*
* The cylinder number field in an INT 13 call is ten bits
* wide, giving a maximum of 1024 cylinders. Conventionally,
* when the 7.8GB limit of a CHS address is exceeded, it is
* the number of cylinders that is increased beyond the
* addressable limit.
*/
unsigned int cylinders;
/** Number of heads
*
* The head number field in an INT 13 call is eight bits wide,
* giving a maximum of 256 heads. However, apparently all
* versions of MS-DOS up to and including Win95 fail with 256
* heads, so the maximum encountered in practice is 255.
*/
unsigned int heads;
/** Number of sectors per track
*
* The sector number field in an INT 13 call is six bits wide,
* giving a maximum of 63 sectors, since sector numbering
* (unlike head and cylinder numbering) starts at 1, not 0.
*/
unsigned int sectors_per_track;
/** Address of El Torito boot catalog (if any) */
unsigned int boot_catalog;
/** Status of last operation */
int last_status;
};
/** Vector for chaining to other INT 13 handlers */
static struct segoff __text16 ( int13_vector );
#define int13_vector __use_text16 ( int13_vector )
/** Assembly wrapper */
extern void int13_wrapper ( void );
/** Dummy floppy disk parameter table */
static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = {
/* 512 bytes per sector */
.bytes_per_sector = 0x02,
/* Highest sectors per track that we ever return */
.sectors_per_track = 48,
};
#define int13_fdd_params __use_data16 ( int13_fdd_params )
/**
* Equipment word
*
* This is a cached copy of the BIOS Data Area equipment word at
* 40:10.
*/
static uint16_t equipment_word;
/**
* Number of BIOS floppy disk drives
*
* This is derived from the equipment word. It is held in .text16 to
* allow for easy access by the INT 13,08 wrapper.
*/
static uint8_t __text16 ( num_fdds );
#define num_fdds __use_text16 ( num_fdds )
/**
* Number of BIOS hard disk drives
*
* This is a cached copy of the BIOS Data Area number of hard disk
* drives at 40:75. It is held in .text16 to allow for easy access by
* the INT 13,08 wrapper.
*/
static uint8_t __text16 ( num_drives );
#define num_drives __use_text16 ( num_drives )
/**
* Calculate SAN device capacity (limited to 32 bits)
*
* @v sandev SAN device
* @ret blocks Number of blocks
*/
static inline uint32_t int13_capacity32 ( struct san_device *sandev ) {
uint64_t capacity = sandev_capacity ( sandev );
return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
}
/**
* Test if SAN device is a floppy disk drive
*
* @v sandev SAN device
* @ret is_fdd SAN device is a floppy disk drive
*/
static inline int int13_is_fdd ( struct san_device *sandev ) {
return ( ! ( sandev->drive & 0x80 ) );
}
/**
* Parse El Torito parameters
*
* @v sandev SAN device
* @v scratch Scratch area for single-sector reads
* @ret rc Return status code
*
* Reads and parses El Torito parameters, if present.
*/
static int int13_parse_eltorito ( struct san_device *sandev, void *scratch ) {
struct int13_data *int13 = sandev->priv;
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;
int rc;
/* Read boot record volume descriptor */
if ( ( rc = sandev_read ( sandev, ELTORITO_LBA, 1,
virt_to_user ( boot ) ) ) != 0 ) {
DBGC ( sandev->drive, "INT13 drive %02x could not read El "
"Torito boot record volume descriptor: %s\n",
sandev->drive, strerror ( rc ) );
return rc;
}
/* Check for an El Torito boot catalog */
if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) {
int13->boot_catalog = boot->sector;
DBGC ( sandev->drive, "INT13 drive %02x has an El Torito boot "
"catalog at LBA %08x\n", sandev->drive,
int13->boot_catalog );
} else {
DBGC ( sandev->drive, "INT13 drive %02x has no El Torito boot "
"catalog\n", sandev->drive );
}
return 0;
}
/**
* Guess INT 13 hard disk drive geometry
*
* @v sandev SAN device
* @v scratch Scratch area for single-sector reads
* @ret heads Guessed number of heads
* @ret sectors Guessed number of sectors per track
* @ret rc Return status code
*
* Guesses the drive geometry by inspecting the partition table.
*/
static int int13_guess_geometry_hdd ( struct san_device *sandev, void *scratch,
unsigned int *heads,
unsigned int *sectors ) {
struct master_boot_record *mbr = scratch;
struct partition_table_entry *partition;
unsigned int i;
unsigned int start_cylinder;
unsigned int start_head;
unsigned int start_sector;
unsigned int end_head;
unsigned int end_sector;
int rc;
/* Read partition table */
if ( ( rc = sandev_read ( sandev, 0, 1, virt_to_user ( mbr ) ) ) != 0 ) {
DBGC ( sandev->drive, "INT13 drive %02x could not read "
"partition table to guess geometry: %s\n",
sandev->drive, strerror ( rc ) );
return rc;
}
DBGC2 ( sandev->drive, "INT13 drive %02x has MBR:\n", sandev->drive );
DBGC2_HDA ( sandev->drive, 0, mbr, sizeof ( *mbr ) );
DBGC ( sandev->drive, "INT13 drive %02x has signature %08x\n",
sandev->drive, mbr->signature );
/* Scan through partition table and modify guesses for
* heads and sectors_per_track if we find any used
* partitions.
*/
*heads = 0;
*sectors = 0;
for ( i = 0 ; i < 4 ; i++ ) {
/* Skip empty partitions */
partition = &mbr->partitions[i];
if ( ! partition->type )
continue;
/* If partition starts on cylinder 0 then we can
* unambiguously determine the number of sectors.
*/
start_cylinder = PART_CYLINDER ( partition->chs_start );
start_head = PART_HEAD ( partition->chs_start );
start_sector = PART_SECTOR ( partition->chs_start );
if ( ( start_cylinder == 0 ) && ( start_head != 0 ) ) {
*sectors = ( ( partition->start + 1 - start_sector ) /
start_head );
DBGC ( sandev->drive, "INT13 drive %02x guessing "
"C/H/S xx/xx/%d based on partition %d\n",
sandev->drive, *sectors, ( i + 1 ) );
}
/* If partition ends on a higher head or sector number
* than our current guess, then increase the guess.
*/
end_head = PART_HEAD ( partition->chs_end );
end_sector = PART_SECTOR ( partition->chs_end );
if ( ( end_head + 1 ) > *heads ) {
*heads = ( end_head + 1 );
DBGC ( sandev->drive, "INT13 drive %02x guessing "
"C/H/S xx/%d/xx based on partition %d\n",
sandev->drive, *heads, ( i + 1 ) );
}
if ( end_sector > *sectors ) {
*sectors = end_sector;
DBGC ( sandev->drive, "INT13 drive %02x guessing "
"C/H/S xx/xx/%d based on partition %d\n",
sandev->drive, *sectors, ( i + 1 ) );
}
}
/* Default guess is xx/255/63 */
if ( ! *heads )
*heads = 255;
if ( ! *sectors )
*sectors = 63;
return 0;
}
/** Recognised floppy disk geometries */
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 ),
};
/**
* Guess INT 13 floppy disk drive geometry
*
* @v sandev SAN device
* @ret heads Guessed number of heads
* @ret sectors Guessed number of sectors per track
* @ret rc Return status code
*
* Guesses the drive geometry by inspecting the disk size.
*/
static int int13_guess_geometry_fdd ( struct san_device *sandev,
unsigned int *heads,
unsigned int *sectors ) {
unsigned int blocks = sandev_capacity ( sandev );
const struct int13_fdd_geometry *geometry;
unsigned int cylinders;
unsigned int i;
/* Look for a match against a known geometry */
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 ( sandev->drive, "INT13 drive %02x guessing "
"C/H/S %d/%d/%d based on size %dK\n",
sandev->drive, cylinders, *heads, *sectors,
( blocks / 2 ) );
return 0;
}
}
/* Otherwise, assume a partial disk image in the most common
* format (1440K, 80/2/18).
*/
*heads = 2;
*sectors = 18;
DBGC ( sandev->drive, "INT13 drive %02x guessing C/H/S xx/%d/%d "
"based on size %dK\n", sandev->drive, *heads, *sectors,
( blocks / 2 ) );
return 0;
}
/**
* Guess INT 13 drive geometry
*
* @v sandev SAN device
* @v scratch Scratch area for single-sector reads
* @ret rc Return status code
*/
static int int13_guess_geometry ( struct san_device *sandev, void *scratch ) {
struct int13_data *int13 = sandev->priv;
unsigned int guessed_heads;
unsigned int guessed_sectors;
unsigned int blocks;
unsigned int blocks_per_cyl;
int rc;
/* Guess geometry according to drive type */
if ( int13_is_fdd ( sandev ) ) {
if ( ( rc = int13_guess_geometry_fdd ( sandev, &guessed_heads,
&guessed_sectors )) != 0)
return rc;
} else {
if ( ( rc = int13_guess_geometry_hdd ( sandev, scratch,
&guessed_heads,
&guessed_sectors )) != 0)
return rc;
}
/* Apply guesses if no geometry already specified */
if ( ! int13->heads )
int13->heads = guessed_heads;
if ( ! int13->sectors_per_track )
int13->sectors_per_track = guessed_sectors;
if ( ! int13->cylinders ) {
/* Avoid attempting a 64-bit divide on a 32-bit system */
blocks = int13_capacity32 ( sandev );
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;
}
/**
* Update BIOS drive count
*/
static void int13_sync_num_drives ( void ) {
struct san_device *sandev;
struct int13_data *int13;
uint8_t *counter;
uint8_t max_drive;
uint8_t required;
/* Get current drive counts */
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 );
/* Ensure count is large enough to cover all of our SAN devices */
for_each_sandev ( sandev ) {
int13 = sandev->priv;
counter = ( int13_is_fdd ( sandev ) ? &num_fdds : &num_drives );
max_drive = sandev->drive;
if ( max_drive < int13->natural_drive )
max_drive = int13->natural_drive;
required = ( ( max_drive & 0x7f ) + 1 );
if ( *counter < required ) {
*counter = required;
DBGC ( sandev->drive, "INT13 drive %02x added to "
"drive count: %d HDDs, %d FDDs\n",
sandev->drive, num_drives, num_fdds );
}
}
/* Update current drive count */
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 );
}
/**
* Check number of 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();
}
}
/**
* INT 13, 00 - Reset disk system
*
* @v sandev SAN device
* @ret status Status code
*/
static int int13_reset ( struct san_device *sandev,
struct i386_all_regs *ix86 __unused ) {
int rc;
DBGC2 ( sandev->drive, "Reset drive\n" );
/* Reset SAN device */
if ( ( rc = sandev_reset ( sandev ) ) != 0 )
return -INT13_STATUS_RESET_FAILED;
return 0;
}
/**
* INT 13, 01 - Get status of last operation
*
* @v sandev SAN device
* @ret status Status code
*/
static int int13_get_last_status ( struct san_device *sandev,
struct i386_all_regs *ix86 __unused ) {
struct int13_data *int13 = sandev->priv;
DBGC2 ( sandev->drive, "Get status of last operation\n" );
return int13->last_status;
}
/**
* Read / write sectors
*
* @v sandev SAN device
* @v al Number of sectors to read or write (must be nonzero)
* @v ch Low bits of cylinder number
* @v cl (bits 7:6) High bits of cylinder number
* @v cl (bits 5:0) Sector number
* @v dh Head number
* @v es:bx Data buffer
* @v sandev_rw SAN device read/write method
* @ret status Status code
* @ret al Number of sectors read or written
*/
static int int13_rw_sectors ( struct san_device *sandev,
struct i386_all_regs *ix86,
int ( * sandev_rw ) ( struct san_device *sandev,
uint64_t lba,
unsigned int count,
userptr_t buffer ) ) {
struct int13_data *int13 = sandev->priv;
unsigned int cylinder, head, sector;
unsigned long lba;
unsigned int count;
userptr_t buffer;
int rc;
/* Validate blocksize */
if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) {
DBGC ( sandev->drive, "\nINT 13 drive %02x invalid blocksize "
"(%zd) for non-extended read/write\n",
sandev->drive, sandev_blksize ( sandev ) );
return -INT13_STATUS_INVALID;
}
/* Calculate parameters */
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 ( sandev->drive, "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 ( sandev->drive, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x "
"(count %d)\n", cylinder, head, sector, lba, ix86->segs.es,
ix86->regs.bx, count );
/* Read from / write to block device */
if ( ( rc = sandev_rw ( sandev, lba, count, buffer ) ) != 0 ){
DBGC ( sandev->drive, "INT13 drive %02x I/O failed: %s\n",
sandev->drive, strerror ( rc ) );
return -INT13_STATUS_READ_ERROR;
}
return 0;
}
/**
* INT 13, 02 - Read sectors
*
* @v sandev SAN device
* @v al Number of sectors to read (must be nonzero)
* @v ch Low bits of cylinder number
* @v cl (bits 7:6) High bits of cylinder number
* @v cl (bits 5:0) Sector number
* @v dh Head number
* @v es:bx Data buffer
* @ret status Status code
* @ret al Number of sectors read
*/
static int int13_read_sectors ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
DBGC2 ( sandev->drive, "Read: " );
return int13_rw_sectors ( sandev, ix86, sandev_read );
}
/**
* INT 13, 03 - Write sectors
*
* @v sandev SAN device
* @v al Number of sectors to write (must be nonzero)
* @v ch Low bits of cylinder number
* @v cl (bits 7:6) High bits of cylinder number
* @v cl (bits 5:0) Sector number
* @v dh Head number
* @v es:bx Data buffer
* @ret status Status code
* @ret al Number of sectors written
*/
static int int13_write_sectors ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
DBGC2 ( sandev->drive, "Write: " );
return int13_rw_sectors ( sandev, ix86, sandev_write );
}
/**
* INT 13, 08 - Get drive parameters
*
* @v sandev SAN device
* @ret status Status code
* @ret ch Low bits of maximum cylinder number
* @ret cl (bits 7:6) High bits of maximum cylinder number
* @ret cl (bits 5:0) Maximum sector number
* @ret dh Maximum head number
* @ret dl Number of drives
*/
static int int13_get_parameters ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
struct int13_data *int13 = sandev->priv;
unsigned int max_cylinder = int13->cylinders - 1;
unsigned int max_head = int13->heads - 1;
unsigned int max_sector = int13->sectors_per_track; /* sic */
DBGC2 ( sandev->drive, "Get drive parameters\n" );
/* Validate blocksize */
if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) {
DBGC ( sandev->drive, "\nINT 13 drive %02x invalid blocksize "
"(%zd) for non-extended parameters\n",
sandev->drive, sandev_blksize ( sandev ) );
return -INT13_STATUS_INVALID;
}
/* Common parameters */
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 ( sandev ) ? num_fdds : num_drives );
/* Floppy-specific parameters */
if ( int13_is_fdd ( sandev ) ) {
ix86->regs.bl = INT13_FDD_TYPE_1M44;
ix86->segs.es = rm_ds;
ix86->regs.di = __from_data16 ( &int13_fdd_params );
}
return 0;
}
/**
* INT 13, 15 - Get disk type
*
* @v sandev SAN device
* @ret ah Type code
* @ret cx:dx Sector count
* @ret status Status code / disk type
*/
static int int13_get_disk_type ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
uint32_t blocks;
DBGC2 ( sandev->drive, "Get disk type\n" );
if ( int13_is_fdd ( sandev ) ) {
return INT13_DISK_TYPE_FDD;
} else {
blocks = int13_capacity32 ( sandev );
ix86->regs.cx = ( blocks >> 16 );
ix86->regs.dx = ( blocks & 0xffff );
return INT13_DISK_TYPE_HDD;
}
}
/**
* INT 13, 41 - Extensions installation check
*
* @v sandev SAN device
* @v bx 0x55aa
* @ret bx 0xaa55
* @ret cx Extensions API support bitmap
* @ret status Status code / API version
*/
static int int13_extension_check ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
if ( ( ix86->regs.bx == 0x55aa ) && ! int13_is_fdd ( sandev ) ) {
DBGC2 ( sandev->drive, "INT13 extensions 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;
}
}
/**
* Extended read / write
*
* @v sandev SAN device
* @v ds:si Disk address packet
* @v sandev_rw SAN device read/write method
* @ret status Status code
*/
static int int13_extended_rw ( struct san_device *sandev,
struct i386_all_regs *ix86,
int ( * sandev_rw ) ( struct san_device *sandev,
uint64_t lba,
unsigned int count,
userptr_t buffer ) ) {
struct int13_disk_address addr;
uint8_t bufsize;
uint64_t lba;
unsigned long count;
userptr_t buffer;
int rc;
/* Extended reads are not allowed on floppy drives.
* ELTORITO.SYS seems to assume that we are really a CD-ROM if
* we support extended reads for a floppy drive.
*/
if ( int13_is_fdd ( sandev ) )
return -INT13_STATUS_INVALID;
/* Get buffer size */
get_real ( bufsize, ix86->segs.ds,
( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
DBGC2 ( sandev->drive, "<invalid buffer size %#02x\n>\n",
bufsize );
return -INT13_STATUS_INVALID;
}
/* Read parameters from disk address structure */
memset ( &addr, 0, sizeof ( addr ) );
copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
lba = addr.lba;
DBGC2 ( sandev->drive, "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 ( sandev->drive, "%08llx",
( ( unsigned long long ) addr.buffer_phys ) );
} else {
buffer = real_to_user ( addr.buffer.segment,
addr.buffer.offset );
DBGC2 ( sandev->drive, "%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 ( sandev->drive, " <invalid count %#02x>\n", addr.count );
return -INT13_STATUS_INVALID;
}
DBGC2 ( sandev->drive, " (count %ld)\n", count );
/* Read from / write to block device */
if ( ( rc = sandev_rw ( sandev, lba, count, buffer ) ) != 0 ) {
DBGC ( sandev->drive, "INT13 drive %02x extended I/O failed: "
"%s\n", sandev->drive, strerror ( rc ) );
/* Record that no blocks were transferred successfully */
addr.count = 0;
put_real ( addr.count, ix86->segs.ds,
( ix86->regs.si +
offsetof ( typeof ( addr ), count ) ) );
return -INT13_STATUS_READ_ERROR;
}
return 0;
}
/**
* INT 13, 42 - Extended read
*
* @v sandev SAN device
* @v ds:si Disk address packet
* @ret status Status code
*/
static int int13_extended_read ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
DBGC2 ( sandev->drive, "Extended read: " );
return int13_extended_rw ( sandev, ix86, sandev_read );
}
/**
* INT 13, 43 - Extended write
*
* @v sandev SAN device
* @v ds:si Disk address packet
* @ret status Status code
*/
static int int13_extended_write ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
DBGC2 ( sandev->drive, "Extended write: " );
return int13_extended_rw ( sandev, ix86, sandev_write );
}
/**
* INT 13, 44 - Verify sectors
*
* @v sandev SAN device
* @v ds:si Disk address packet
* @ret status Status code
*/
static int int13_extended_verify ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
struct int13_disk_address addr;
uint64_t lba;
unsigned long count;
/* Read parameters from disk address structure */
if ( DBG_EXTRA ) {
copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
sizeof ( addr ));
lba = addr.lba;
count = addr.count;
DBGC2 ( sandev->drive, "Verify: LBA %08llx (count %ld)\n",
( ( unsigned long long ) lba ), count );
}
/* We have no mechanism for verifying sectors */
return -INT13_STATUS_INVALID;
}
/**
* INT 13, 44 - Extended seek
*
* @v sandev SAN device
* @v ds:si Disk address packet
* @ret status Status code
*/
static int int13_extended_seek ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
struct int13_disk_address addr;
uint64_t lba;
unsigned long count;
/* Read parameters from disk address structure */
if ( DBG_EXTRA ) {
copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
sizeof ( addr ));
lba = addr.lba;
count = addr.count;
DBGC2 ( sandev->drive, "Seek: LBA %08llx (count %ld)\n",
( ( unsigned long long ) lba ), count );
}
/* Ignore and return success */
return 0;
}
/**
* Build device path information
*
* @v sandev SAN device
* @v dpi Device path information
* @ret rc Return status code
*/
static int int13_device_path_info ( struct san_device *sandev,
struct edd_device_path_information *dpi ) {
struct san_path *sanpath;
struct device *device;
struct device_description *desc;
unsigned int i;
uint8_t sum = 0;
int rc;
/* Reopen block device if necessary */
if ( sandev_needs_reopen ( sandev ) &&
( ( rc = sandev_reopen ( sandev ) ) != 0 ) )
return rc;
sanpath = sandev->active;
assert ( sanpath != NULL );
/* Get underlying hardware device */
device = identify_device ( &sanpath->block );
if ( ! device ) {
DBGC ( sandev->drive, "INT13 drive %02x cannot identify "
"hardware device\n", sandev->drive );
return -ENODEV;
}
/* Fill in bus type and interface path */
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; /* unused */
break;
default:
DBGC ( sandev->drive, "INT13 drive %02x unrecognised bus "
"type %d\n", sandev->drive, desc->bus_type );
return -ENOTSUP;
}
/* Get EDD block device description */
if ( ( rc = edd_describe ( &sanpath->block, &dpi->interface_type,
&dpi->device_path ) ) != 0 ) {
DBGC ( sandev->drive, "INT13 drive %02x cannot identify "
"block device: %s\n", sandev->drive, strerror ( rc ) );
return rc;
}
/* Fill in common fields and fix checksum */
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;
}
/**
* INT 13, 48 - Get extended parameters
*
* @v sandev SAN device
* @v ds:si Drive parameter table
* @ret status Status code
*/
static int int13_get_extended_parameters ( struct san_device *sandev,
struct i386_all_regs *ix86 ) {
struct int13_data *int13 = sandev->priv;
struct int13_disk_parameters params;
struct segoff address;
size_t len = sizeof ( params );
uint16_t bufsize;
int rc;
/* Get buffer size */
get_real ( bufsize, ix86->segs.ds,
( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
DBGC2 ( sandev->drive, "Get extended drive parameters to "
"%04x:%04x+%02x\n", ix86->segs.ds, ix86->regs.si, bufsize );
/* Build drive parameters */
memset ( ¶ms, 0, sizeof ( params ) );
params.flags = INT13_FL_DMA_TRANSPARENT;
if ( ( int13->cylinders < 1024 ) &&
( sandev_capacity ( sandev ) <= 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 = sandev_capacity ( sandev );
params.sector_size = sandev_blksize ( sandev );
memset ( ¶ms.dpte, 0xff, sizeof ( params.dpte ) );
if ( ( rc = int13_device_path_info ( sandev, ¶ms.dpi ) ) != 0 ) {
DBGC ( sandev->drive, "INT13 drive %02x could not provide "
"device path information: %s\n",
sandev->drive, strerror ( rc ) );
len = offsetof ( typeof ( params ), dpi );
}
/* Calculate returned "buffer size" (which will be less than
* the length actually copied if device path information is
* present).
*/
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 ( sandev->drive, "INT 13 drive %02x described using extended "
"parameters:\n", sandev->drive );
address.segment = ix86->segs.ds;
address.offset = ix86->regs.si;
DBGC_HDA ( sandev->drive, address, ¶ms, len );
/* Return drive parameters */
if ( len > bufsize )
len = bufsize;
copy_to_real ( ix86->segs.ds, ix86->regs.si, ¶ms, len );
return 0;
}
/**
* INT 13, 4b - Get status or terminate CD-ROM emulation
*
* @v sandev SAN device
* @v ds:si Specification packet