#include /* To: Judy Polonsky From: Jerry Armstrong, NCR - Personal Computer Division Subject: Physical to/from logical block address conversion and algorithm to determine mapping of scsi drives into int 13h head cylinder sector format. These "C" routines are modifications of the ones developed by Patrick Manley (SyQuest Technology). This algorithm has most (if not all) of the characteristics that we discussed at the February CAM meeting. I used ANSI "C" that can be compiled using the Microsoft C compiler, version 5.1. 1. Setsize converts a READ CAPACITY value to int 13h head/cylinder/sector requirements. Note that it minimizes the value for number of heads and maximizes the number of cylinders. This will allow us to support rather large disks before the number of heads will not fit in 4 bits (or 6 bits). This algorithm also seems to minimize the number of sectors that will be unused at the end of the disk while allowing for very large disks to be accomodated. Also note that this algorithm does NOT use physical geometry. 2. ltop does logical to physical conversion 3. ptol does physical to logical conversion 4. main is a test routine for 1, 2, & 3 */ typedef unsigned int UINT; typedef unsigned long ULNG; /* * Convert from logical block count to Cylinder Sector and head (int 13) */ int setsize(ULNG capacity,UINT *cyls,UINT *hds,UINT *secs) { UINT rv = 0; ULNG heads, sectors, cylinders, temp; cylinders = 1024L; /* Set number of cylinders to max value. */ sectors = 62L; /* Max out number of sectors per track. */ temp = cylinders * sectors; /* Compute divisor for heads. */ heads = capacity / temp; /* Compute value for number of heads. */ if (capacity % temp) { /* If no remainder, done! */ heads++; /* Else, increment number of heads. */ temp = cylinders * heads; /* Compute divisor for sectors. */ sectors = capacity / temp; /* Compute value for sectors per track. */ if (capacity % temp) { /* If no remainder, done! */ sectors++; /* Else, increment number of sectors. */ temp = heads * sectors; /* Compute divisor for cylinders. */ cylinders = capacity / temp; /* Compute number of cylinders. */ } } if (cylinders == 0) rv=1; /* Give error if 0 cylinders. */ *cyls = (UINT) cylinders; /* Stuff return values. */ *secs = (UINT) sectors; *hds = (UINT) heads; return(rv); } /* * logical to physical conversion */ void ltop(ULNG block,UINT hd_count,UINT sec_count,UINT *cyl,UINT *hd,UINT *sec) { UINT spc; spc = hd_count * sec_count; *cyl = block / spc; *hd = (block % spc) / sec_count; *sec = (block % spc) % sec_count; } /* * Physical to logical conversion */ ULNG ptol(UINT cyl,UINT hd,UINT sec,UINT cyl_count,UINT hd_count,UINT sec_count) { ULNG cylsize; cylsize = sec_count * hd_count; return((cyl * cylsize) + (hd * sec_count) + sec); } /* * Test routine */ void main(void) { ULNG mblock,block,block1,t; UINT c,h,s,v; UINT cyl,hd,sec; UINT mb; printf("Block translation test\n"); /* Allow testing of various device sizes and addresses */ while (1) { printf("\nEnter Max block number:"); scanf("%lu",&mblock); if (mblock) { v = setsize(mblock,&c,&h,&s); t = ((ULNG) c) * h * ((ULNG) s); printf("Max blk %lu, C:%u H:%u S:%u Cap:%lu Waste:%lu RV:%d\n", mblock,c,h,s,t,(mblock - t),v); } /* end if mblock */ else break; do { printf("\tEnter block number:"); scanf("%lu",&block); if (block) { ltop(block,h,s,&cyl,&hd,&sec); block1 = ptol(cyl,hd,sec,c,h,s); printf("\tblock %8lu becomes C:%4u H:%3u S:%3u becomes %8lu\n", block,cyl,hd,sec,block1); } /* end if block */ } /* end do */ while (block); } /* end while true */ /* now we show all translations */ printf(" Address Conversion Table for int 13\n"); for(mb = 10; mb <= 1200; mb += 10) { block = (ULNG)mb * 2048L; /* convert megabytes to sector count. */ v = setsize(block,&c,&h,&s); t = ((ULNG) c) * h * ((ULNG) s); printf("%4dMB=Max:%8lu, C:%4u H:%3u S:%3u Cap:%8lu Waste:%8lu RV:%d\n", mb,block,c,h,s,t,(block - t),v); } /* end for */ } /* end main routine */ /* The following are the results of running this program: Address Conversion Table for int 13 10MB=Max: 20480, C:1024 H: 1 S: 20 Cap: 20480 Waste: 0 RV:0 20MB=Max: 40960, C:1024 H: 1 S: 40 Cap: 40960 Waste: 0 RV:0 30MB=Max: 61440, C:1024 H: 1 S: 60 Cap: 61440 Waste: 0 RV:0 40MB=Max: 81920, C:1024 H: 2 S: 40 Cap: 81920 Waste: 0 RV:0 50MB=Max: 102400, C:1024 H: 2 S: 50 Cap: 102400 Waste: 0 RV:0 60MB=Max: 122880, C:1024 H: 2 S: 60 Cap: 122880 Waste: 0 RV:0 70MB=Max: 143360, C:1016 H: 3 S: 47 Cap: 143256 Waste: 104 RV:0 80MB=Max: 163840, C:1011 H: 3 S: 54 Cap: 163782 Waste: 58 RV:0 90MB=Max: 184320, C:1024 H: 3 S: 60 Cap: 184320 Waste: 0 RV:0 100MB=Max: 204800, C:1024 H: 4 S: 50 Cap: 204800 Waste: 0 RV:0 110MB=Max: 225280, C:1024 H: 4 S: 55 Cap: 225280 Waste: 0 RV:0 120MB=Max: 245760, C:1024 H: 4 S: 60 Cap: 245760 Waste: 0 RV:0 130MB=Max: 266240, C:1024 H: 5 S: 52 Cap: 266240 Waste: 0 RV:0 140MB=Max: 286720, C:1024 H: 5 S: 56 Cap: 286720 Waste: 0 RV:0 150MB=Max: 307200, C:1024 H: 5 S: 60 Cap: 307200 Waste: 0 RV:0 160MB=Max: 327680, C:1011 H: 6 S: 54 Cap: 327564 Waste: 116 RV:0 170MB=Max: 348160, C:1018 H: 6 S: 57 Cap: 348156 Waste: 4 RV:0 180MB=Max: 368640, C:1024 H: 6 S: 60 Cap: 368640 Waste: 0 RV:0 190MB=Max: 389120, C:1010 H: 7 S: 55 Cap: 388850 Waste: 270 RV:0 200MB=Max: 409600, C:1008 H: 7 S: 58 Cap: 409248 Waste: 352 RV:0 210MB=Max: 430080, C:1024 H: 7 S: 60 Cap: 430080 Waste: 0 RV:0 220MB=Max: 450560, C:1024 H: 8 S: 55 Cap: 450560 Waste: 0 RV:0 230MB=Max: 471040, C:1015 H: 8 S: 58 Cap: 470960 Waste: 80 RV:0 240MB=Max: 491520, C:1024 H: 8 S: 60 Cap: 491520 Waste: 0 RV:0 250MB=Max: 512000, C:1015 H: 9 S: 56 Cap: 511560 Waste: 440 RV:0 260MB=Max: 532480, C:1020 H: 9 S: 58 Cap: 532440 Waste: 40 RV:0 270MB=Max: 552960, C:1024 H: 9 S: 60 Cap: 552960 Waste: 0 RV:0 280MB=Max: 573440, C:1024 H: 10 S: 56 Cap: 573440 Waste: 0 RV:0 290MB=Max: 593920, C:1024 H: 10 S: 58 Cap: 593920 Waste: 0 RV:0 300MB=Max: 614400, C:1024 H: 10 S: 60 Cap: 614400 Waste: 0 RV:0 310MB=Max: 634880, C:1024 H: 10 S: 62 Cap: 634880 Waste: 0 RV:0 320MB=Max: 655360, C:1009 H: 11 S: 59 Cap: 654841 Waste: 519 RV:0 330MB=Max: 675840, C:1024 H: 11 S: 60 Cap: 675840 Waste: 0 RV:0 340MB=Max: 696320, C:1020 H: 11 S: 62 Cap: 695640 Waste: 680 RV:0 350MB=Max: 716800, C:1012 H: 12 S: 59 Cap: 716496 Waste: 304 RV:0 360MB=Max: 737280, C:1024 H: 12 S: 60 Cap: 737280 Waste: 0 RV:0 370MB=Max: 757760, C:1018 H: 12 S: 62 Cap: 757392 Waste: 368 RV:0 380MB=Max: 778240, C:1014 H: 13 S: 59 Cap: 777738 Waste: 502 RV:0 390MB=Max: 798720, C:1024 H: 13 S: 60 Cap: 798720 Waste: 0 RV:0 400MB=Max: 819200, C:1016 H: 13 S: 62 Cap: 818896 Waste: 304 RV:0 410MB=Max: 839680, C:1016 H: 14 S: 59 Cap: 839216 Waste: 464 RV:0 420MB=Max: 860160, C:1024 H: 14 S: 60 Cap: 860160 Waste: 0 RV:0 430MB=Max: 880640, C:1014 H: 14 S: 62 Cap: 880152 Waste: 488 RV:0 440MB=Max: 901120, C:1018 H: 15 S: 59 Cap: 900930 Waste: 190 RV:0 450MB=Max: 921600, C:1024 H: 15 S: 60 Cap: 921600 Waste: 0 RV:0 460MB=Max: 942080, C:1012 H: 15 S: 62 Cap: 941160 Waste: 920 RV:0 470MB=Max: 962560, C:1019 H: 16 S: 59 Cap: 961936 Waste: 624 RV:0 480MB=Max: 983040, C:1024 H: 16 S: 60 Cap: 983040 Waste: 0 RV:0 490MB=Max: 1003520, C:1011 H: 16 S: 62 Cap: 1002912 Waste: 608 RV:0 500MB=Max: 1024000, C:1020 H: 17 S: 59 Cap: 1023060 Waste: 940 RV:0 510MB=Max: 1044480, C:1024 H: 17 S: 60 Cap: 1044480 Waste: 0 RV:0 520MB=Max: 1064960, C:1010 H: 17 S: 62 Cap: 1064540 Waste: 420 RV:0 530MB=Max: 1085440, C:1022 H: 18 S: 59 Cap: 1085364 Waste: 76 RV:0 540MB=Max: 1105920, C:1024 H: 18 S: 60 Cap: 1105920 Waste: 0 RV:0 550MB=Max: 1126400, C:1009 H: 18 S: 62 Cap: 1126044 Waste: 356 RV:0 560MB=Max: 1146880, C:1023 H: 19 S: 59 Cap: 1146783 Waste: 97 RV:0 570MB=Max: 1167360, C:1024 H: 19 S: 60 Cap: 1167360 Waste: 0 RV:0 580MB=Max: 1187840, C:1008 H: 19 S: 62 Cap: 1187424 Waste: 416 RV:0 590MB=Max: 1208320, C:1024 H: 20 S: 59 Cap: 1208320 Waste: 0 RV:0 600MB=Max: 1228800, C:1024 H: 20 S: 60 Cap: 1228800 Waste: 0 RV:0 610MB=Max: 1249280, C:1024 H: 20 S: 61 Cap: 1249280 Waste: 0 RV:0 620MB=Max: 1269760, C:1024 H: 20 S: 62 Cap: 1269760 Waste: 0 RV:0 630MB=Max: 1290240, C:1024 H: 21 S: 60 Cap: 1290240 Waste: 0 RV:0 640MB=Max: 1310720, C:1023 H: 21 S: 61 Cap: 1310463 Waste: 257 RV:0 650MB=Max: 1331200, C:1022 H: 21 S: 62 Cap: 1330644 Waste: 556 RV:0 660MB=Max: 1351680, C:1024 H: 22 S: 60 Cap: 1351680 Waste: 0 RV:0 670MB=Max: 1372160, C:1022 H: 22 S: 61 Cap: 1371524 Waste: 636 RV:0 680MB=Max: 1392640, C:1020 H: 22 S: 62 Cap: 1391280 Waste: 1360 RV:0 690MB=Max: 1413120, C:1024 H: 23 S: 60 Cap: 1413120 Waste: 0 RV:0 700MB=Max: 1433600, C:1021 H: 23 S: 61 Cap: 1432463 Waste: 1137 RV:0 710MB=Max: 1454080, C:1019 H: 23 S: 62 Cap: 1453094 Waste: 986 RV:0 720MB=Max: 1474560, C:1024 H: 24 S: 60 Cap: 1474560 Waste: 0 RV:0 730MB=Max: 1495040, C:1021 H: 24 S: 61 Cap: 1494744 Waste: 296 RV:0 740MB=Max: 1515520, C:1018 H: 24 S: 62 Cap: 1514784 Waste: 736 RV:0 750MB=Max: 1536000, C:1024 H: 25 S: 60 Cap: 1536000 Waste: 0 RV:0 760MB=Max: 1556480, C:1020 H: 25 S: 61 Cap: 1555500 Waste: 980 RV:0 770MB=Max: 1576960, C:1017 H: 25 S: 62 Cap: 1576350 Waste: 610 RV:0 780MB=Max: 1597440, C:1024 H: 26 S: 60 Cap: 1597440 Waste: 0 RV:0 790MB=Max: 1617920, C:1020 H: 26 S: 61 Cap: 1617720 Waste: 200 RV:0 800MB=Max: 1638400, C:1016 H: 26 S: 62 Cap: 1637792 Waste: 608 RV:0 810MB=Max: 1658880, C:1024 H: 27 S: 60 Cap: 1658880 Waste: 0 RV:0 820MB=Max: 1679360, C:1019 H: 27 S: 61 Cap: 1678293 Waste: 1067 RV:0 830MB=Max: 1699840, C:1015 H: 27 S: 62 Cap: 1699110 Waste: 730 RV:0 840MB=Max: 1720320, C:1024 H: 28 S: 60 Cap: 1720320 Waste: 0 RV:0 850MB=Max: 1740800, C:1019 H: 28 S: 61 Cap: 1740452 Waste: 348 RV:0 860MB=Max: 1761280, C:1014 H: 28 S: 62 Cap: 1760304 Waste: 976 RV:0 870MB=Max: 1781760, C:1024 H: 29 S: 60 Cap: 1781760 Waste: 0 RV:0 880MB=Max: 1802240, C:1018 H: 29 S: 61 Cap: 1800842 Waste: 1398 RV:0 890MB=Max: 1822720, C:1013 H: 29 S: 62 Cap: 1821374 Waste: 1346 RV:0 900MB=Max: 1843200, C:1024 H: 30 S: 60 Cap: 1843200 Waste: 0 RV:0 910MB=Max: 1863680, C:1018 H: 30 S: 61 Cap: 1862940 Waste: 740 RV:0 920MB=Max: 1884160, C:1012 H: 30 S: 62 Cap: 1882320 Waste: 1840 RV:0 930MB=Max: 1904640, C:1024 H: 30 S: 62 Cap: 1904640 Waste: 0 RV:0 940MB=Max: 1925120, C:1018 H: 31 S: 61 Cap: 1925038 Waste: 82 RV:0 950MB=Max: 1945600, C:1012 H: 31 S: 62 Cap: 1945064 Waste: 536 RV:0 960MB=Max: 1966080, C:1022 H: 31 S: 62 Cap: 1964284 Waste: 1796 RV:0 970MB=Max: 1986560, C:1017 H: 32 S: 61 Cap: 1985184 Waste: 1376 RV:0 980MB=Max: 2007040, C:1011 H: 32 S: 62 Cap: 2005824 Waste: 1216 RV:0 990MB=Max: 2027520, C:1021 H: 32 S: 62 Cap: 2025664 Waste: 1856 RV:0 1000MB=Max: 2048000, C:1017 H: 33 S: 61 Cap: 2047221 Waste: 779 RV:0 1010MB=Max: 2068480, C:1010 H: 33 S: 62 Cap: 2066460 Waste: 2020 RV:0 1020MB=Max: 2088960, C:1020 H: 33 S: 62 Cap: 2086920 Waste: 2040 RV:0 1030MB=Max: 2109440, C:1017 H: 34 S: 61 Cap: 2109258 Waste: 182 RV:0 1040MB=Max: 2129920, C:1010 H: 34 S: 62 Cap: 2129080 Waste: 840 RV:0 1050MB=Max: 2150400, C:1020 H: 34 S: 62 Cap: 2150160 Waste: 240 RV:0 1060MB=Max: 2170880, C:1016 H: 35 S: 61 Cap: 2169160 Waste: 1720 RV:0 1070MB=Max: 2191360, C:1009 H: 35 S: 62 Cap: 2189530 Waste: 1830 RV:0 1080MB=Max: 2211840, C:1019 H: 35 S: 62 Cap: 2211230 Waste: 610 RV:0 1090MB=Max: 2232320, C:1016 H: 36 S: 61 Cap: 2231136 Waste: 1184 RV:0 1100MB=Max: 2252800, C:1009 H: 36 S: 62 Cap: 2252088 Waste: 712 RV:0 1110MB=Max: 2273280, C:1018 H: 36 S: 62 Cap: 2272176 Waste: 1104 RV:0 1120MB=Max: 2293760, C:1016 H: 37 S: 61 Cap: 2293112 Waste: 648 RV:0 1130MB=Max: 2314240, C:1008 H: 37 S: 62 Cap: 2312352 Waste: 1888 RV:0 1140MB=Max: 2334720, C:1017 H: 37 S: 62 Cap: 2332998 Waste: 1722 RV:0 1150MB=Max: 2355200, C:1016 H: 38 S: 61 Cap: 2355088 Waste: 112 RV:0 1160MB=Max: 2375680, C:1008 H: 38 S: 62 Cap: 2374848 Waste: 832 RV:0 1170MB=Max: 2396160, C:1017 H: 38 S: 62 Cap: 2396052 Waste: 108 RV:0 1180MB=Max: 2416640, C:1015 H: 39 S: 61 Cap: 2414685 Waste: 1955 RV:0 1190MB=Max: 2437120, C:1007 H: 39 S: 62 Cap: 2434926 Waste: 2194 RV:0 1200MB=Max: 2457600, C:1016 H: 39 S: 62 Cap: 2456688 Waste: 912 RV:0 */