Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rom-scan.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #ifndef __TURBOC__
  6. #include <sys/mman.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #endif
  11. #ifdef __TURBOC__
  12. #define HUGE huge
  13. #else
  14. #define HUGE
  15. #endif
  16. #define ROMSTART 0xC8000
  17. #define ROMEND 0xE8000
  18. #define ROMINCREMENT 0x00800
  19. #define ROMMASK 0x03FFF
  20. #ifndef MAP_FAILED
  21. #define MAP_FAILED ((void *)(long long)-1)
  22. #endif
  23. typedef struct Images {
  24. struct Images *next;
  25. long start;
  26. long size;
  27. } Images;
  28. static void rom_scan(const unsigned char HUGE *rom,long offset,long len)
  29. {
  30. static Images *images = NULL;
  31. Images *ptr;
  32. /* The assignments to dummy are to overcome a bug in TurboC */
  33. long dummy, size;
  34. int chksum;
  35. long i;
  36. if (rom[offset] != 0x55 || rom[dummy = offset+1] != 0xAA)
  37. return;
  38. size = (long)rom[dummy = offset+2]*512L;
  39. printf("Found ROM header at %04lX:0000; "
  40. "announces %ldk image (27C%02d EPROM)\n",
  41. offset/16,(size+512)/1024,
  42. size <= 1024 ? 8 :
  43. size <= 2048 ? 16 :
  44. size <= 4096 ? 32 :
  45. size <= 8192 ? 64 :
  46. size <= 16384 ? 128 :
  47. size <= 32768 ? 256 :
  48. size <= 65536 ? 512 : 11);
  49. if (offset & ROMMASK)
  50. printf(" This is a unusual position; not all BIOSs might find it.\n"
  51. " Try to move to a 16kB boundary.\n");
  52. if (size > len) {
  53. printf(" This image extends beyond %04X:0000. "
  54. "It clashes with the system BIOS\n",
  55. ROMEND/16);
  56. size = len; }
  57. for (chksum=0, i = size; i--; chksum += rom[dummy = offset+i]);
  58. if (chksum % 256)
  59. printf(" Checksum does not match. This image is not active\n");
  60. ptr = malloc(sizeof(Images));
  61. ptr->next = images;
  62. ptr->start = offset;
  63. ptr->size = size;
  64. images = ptr;
  65. for (ptr = ptr->next; ptr != NULL; ptr = ptr->next) {
  66. for (i = 0; i < size && i < ptr->size; i++)
  67. if (rom[dummy = ptr->start+i] != rom[dummy = offset+i])
  68. break;
  69. if (i > 32) {
  70. printf(" Image is identical with image at %04lX:0000 "
  71. "for the first %ld bytes\n",
  72. ptr->start/16,i);
  73. if (i >= 1024)
  74. if (i == size)
  75. printf(" this means that you misconfigured the EPROM size!\n");
  76. else
  77. printf(" this could suggest that you misconfigured the "
  78. "EPROM size\n");
  79. else
  80. printf(" this is probably harmless. Just ignore it...\n"); } }
  81. return;
  82. }
  83. int main(void)
  84. {
  85. long i;
  86. unsigned char HUGE *rom;
  87. #ifndef __TURBOC__
  88. int fh;
  89. if ((fh = open("/dev/kmem",O_RDONLY|O_SYNC)) < 0) {
  90. fprintf(stderr,"Could not open \"/dev/kmem\": %s\n",0 );//strerror(errno));
  91. return(1); }
  92. if ((rom = mmap(NULL,ROMEND-ROMSTART,PROT_READ,MAP_SHARED,fh,
  93. ROMSTART)) == MAP_FAILED) {
  94. fprintf(stderr,"Could not mmap \"/dev/kmem\": %s\n",0); //strerror(errno));
  95. close(fh);
  96. return(1); }
  97. close(fh);
  98. #endif
  99. for (i = ROMEND; (i -= ROMINCREMENT) >= ROMSTART; )
  100. #ifdef __TURBOC__
  101. rom_scan(0,i,ROMEND-i);
  102. #else
  103. rom_scan(rom-ROMSTART,i,ROMEND-i);
  104. munmap(rom,ROMEND-ROMSTART);
  105. #endif
  106. return(0);
  107. }