11// written for protected mode
22#include <fuzzy/kernel/process/process.h>
3+ #include <fuzzy/memmgr/tables/gdt.h>
4+ #include <fuzzy/memmgr/layout.h>
35
46#include <lib/utils/logging.h>
57#include <fuzzy/real_mode/client.h>
68
7- int load_sectors (unsigned int full_address ,
8- unsigned char drive ,
9- unsigned int lba , // 0-based
10- unsigned char count ) {
11- int es = (full_address & 0xF0000 )>>4 ;
12- int es_address = full_address & 0xFFFF ;
9+ #define SECTOR_SIZE 512
10+
11+ static int load_sector_via_reallibrary_data (
12+ unsigned int dst_address ,
13+ unsigned char drive ,
14+ unsigned int lba ,
15+ unsigned char count ) {
16+ // assumes count <= 128
17+ int es = (MEMORY_REALLIBRARY_DATA_ADDRESS & 0xF0000 )>>4 ;
18+ int es_address = MEMORY_REALLIBRARY_DATA_ADDRESS & 0xFFFF ;
1319 int cylinder_head = (lba /63 );
1420 int sector_index = lba %63 + 1 ;
1521 // https://en.wikipedia.org/wiki/INT_13H#INT_13h_AH=02h:_Read_Sectors_From_Drive
@@ -28,13 +34,46 @@ int load_sectors(unsigned int full_address,
2834 drive ,
2935 0
3036 );
31- unsigned int status = eax >> 8 ;
32- print_info ("[load_sectors] [dev %x]:%x -> mem:%x, cnt: %d; err: %x" ,
33- drive , lba , full_address , count , status );
34- if (status == 0 ) {
35- // no error copy from temporary address to dst address.
36- // TODO: redefine GDT table order.
37- // kernel_memncpy_absolute(int dst_ds, char *dst_address, int src_ds, char *src_address, size_t size);
37+ unsigned int err = eax >> 8 ;
38+ if (err ) return err ;
39+
40+ // copy from temporary memory to real dst address.
41+ int dst_ds = GDT_ABS32_DS ;
42+ int src_ds = GDT_ABS32_DS ;
43+
44+ kernel_memncpy_absolute (
45+ dst_ds ,
46+ dst_address ,
47+ src_ds ,
48+ MEMORY_REALLIBRARY_DATA_ADDRESS ,
49+ SECTOR_SIZE * (int )count );
50+ print_info ("[load_sectors] mem %x:%x -> mem %x:%x, cnt: %d; err: %d" ,
51+ src_ds , MEMORY_REALLIBRARY_DATA_ADDRESS , dst_ds , dst_address , SECTOR_SIZE * (int )count , 0 );
52+
53+ return 0 ;
54+ };
55+
56+ int load_sectors (unsigned int dst_address ,
57+ unsigned char drive ,
58+ unsigned int lba , // 0-based
59+ unsigned int sector_count ) {
60+ // load_sector_via_reallibrary_data supports upto 255 sector count
61+ // but the memory allocation for reallibrary data is 0x10000 which
62+ // amounts to 128 sectors. Please have a look at memory_layout.md
63+ const int OCC_MAX_SECTOR_COUNT = 128 ;
64+
65+ int err = 0 ;
66+ while (!err && sector_count > 0 ) {
67+ int occ_read_sectors = min (sector_count , OCC_MAX_SECTOR_COUNT );
68+ err = load_sector_via_reallibrary_data (
69+ dst_address ,
70+ drive ,
71+ lba ,
72+ occ_read_sectors
73+ );
74+ dst_address += SECTOR_SIZE * occ_read_sectors ;
75+ lba += occ_read_sectors ;
76+ sector_count -= occ_read_sectors ;
3877 }
39- return status ;
78+ return err ;
4079}
0 commit comments