Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #!/usr/local/bin/tcc -run
  2.  
  3. /*
  4. * lbainfo.c -- print information about a given LBA
  5. * jrra 2012
  6. *
  7. * Usage: ./lbainfo.c <disk type> <LBA>
  8. *
  9. * Example run:
  10. * > ./lbainfo.c 3 833
  11. * Disk type: 3
  12. * LBA: 833
  13. * pzone: 2
  14. * vzone: 2
  15. * size: 17680 bytes
  16. * offset: 15472720 bytes
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <errno.h>
  21.  
  22. int pzone_tbl1[7][16] = {
  23. /*disk type 0*/ {267, 559, 833, 1125, 1417, 1691, 1965, 2239, 2513, 2717, 2921, 3195, 3469, 3743, 4017, 4291,},
  24. /*disk type 1*/ {267, 559, 833, 1107, 1381, 1673, 1965, 2239, 2513, 2787, 2991, 3195, 3469, 3743, 4017, 4291,},
  25. /*disk type 2*/ {267, 559, 833, 1107, 1381, 1655, 1929, 2221, 2513, 2787, 3061, 3265, 3469, 3743, 4017, 4291,},
  26. /*disk type 3*/ {267, 559, 833, 1107, 1381, 1655, 1929, 2203, 2477, 2769, 3061, 3335, 3539, 3743, 4017, 4291,},
  27. /*disk type 4*/ {267, 559, 833, 1107, 1381, 1655, 1929, 2203, 2477, 2751, 3025, 3317, 3609, 3813, 4017, 4291,},
  28. /*disk type 5*/ {267, 559, 833, 1107, 1381, 1655, 1929, 2133, 2407, 2681, 2955, 3229, 3503, 3795, 4087, 4291,},
  29. /*disk type 6*/ {267, 559, 833, 1107, 1381, 1655, 1929, 2133, 2337, 2611, 2885, 3159, 3433, 3707, 3999, 4291,},
  30. };
  31.  
  32. int pzone_tbl2[7][16] = {
  33. /*disk type 0*/ { 0, 1, 2, 14, 15, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,},
  34. /*disk type 1*/ { 0, 1, 2, 3, 13, 14, 15, 4, 5, 6, 7, 8, 9, 10, 11, 12,},
  35. /*disk type 2*/ { 0, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10, 11,},
  36. /*disk type 3*/ { 0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 6, 7, 8, 9, 10,},
  37. /*disk type 4*/ { 0, 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 7, 8, 9,},
  38. /*disk type 5*/ { 0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 8,},
  39. /*disk type 6*/ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,},
  40. };
  41.  
  42. int vzone_tbl[16] = {
  43. 0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1,
  44. };
  45.  
  46. int block_size_per_vzone[9] = {
  47. 19720, 18360, 17680, 16320, 14960, 13600, 12240, 10880, 9520,
  48. };
  49.  
  50.  
  51. int lba_to_pzone( int type, int lba )
  52. {
  53. int pzone = 0;
  54. int i;
  55. for( i=15; i>=0; i-- )
  56. if( lba <= pzone_tbl1[type][i] )
  57. pzone = pzone_tbl2[type][i];
  58. else
  59. break;
  60.  
  61. return pzone;
  62. }
  63.  
  64. int pzone_to_vzone( int pzone )
  65. {
  66. return vzone_tbl[ pzone ];
  67. }
  68.  
  69. int size_of_lba( int type, int lba )
  70. {
  71. return block_size_per_vzone[ pzone_to_vzone( lba_to_pzone( type, lba ) ) ];
  72. }
  73.  
  74. int size_of_sectors( int type, int lba )
  75. {
  76. return size_of_lba( type, lba ) / 85;
  77. }
  78.  
  79. int lba_to_offset( int type, int lba )
  80. {
  81. int offset = 0;
  82. int i;
  83. for( i = 0; i<lba; i++ )
  84. offset += size_of_lba( type, i );
  85. return offset;
  86. }
  87.  
  88.  
  89. int main( int argc, char* argv[] )
  90. {
  91. if( argc < 3 )
  92. {
  93. printf("Usage: lbainfo <disk type> <LBA>\n");
  94. return 1;
  95. }
  96.  
  97. errno = 0;
  98. int type = (int)strtol( argv[1], NULL, 0 );
  99. if( type > 6 || type < 0 || errno != 0 )
  100. {
  101. printf("Disk type must be between 0 and 6\n");
  102. return 2;
  103. }
  104.  
  105. errno = 0;
  106. int lba = (int)strtol( argv[2], NULL, 0 );
  107. if( lba > 4291 || lba < 0 || errno != 0 )
  108. {
  109. printf("LBA must be between 0 and 4291\n");
  110. return 3;
  111. }
  112.  
  113. printf("Disk type: %d\n", type);
  114. printf("LBA: %d\n", lba);
  115.  
  116. int pzone;
  117. int vzone;
  118. int size;
  119. int secsize;
  120. int offset;
  121.  
  122. // First, let's figure out the pzone.
  123. pzone = lba_to_pzone( type, lba );
  124. printf("pzone: %d\n", pzone);
  125.  
  126. // Let's figure out the vzone.
  127. vzone = pzone_to_vzone( pzone );
  128. printf("vzone: %d\n", vzone);
  129.  
  130. // Size of this block.
  131. size = size_of_lba( type, lba );
  132. printf("block size: %d bytes\n", size);
  133.  
  134. // Size of sectors in this block.
  135. secsize = size_of_sectors( type, lba );
  136. printf("sector size: %d bytes\n", secsize);
  137.  
  138. // Let's figure out the offset.
  139. offset = lba_to_offset( type, lba );
  140. printf("offset: %d bytes\n", offset);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement