[3ae31e9] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | check.c -- checksum and stat an absolute format Alcyon object file
|
---|
| 4 | Version 8 -- 1988-10-06 -- D.N. Lynx Crowe
|
---|
| 5 | ============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "stdio.h"
|
---|
| 9 | #include "stddefs.h"
|
---|
| 10 | #include "portab.h"
|
---|
| 11 | #include "objdefs.h"
|
---|
| 12 | #include "biosdefs.h"
|
---|
| 13 |
|
---|
| 14 | extern FILE *fopenb();
|
---|
| 15 | extern int fclose(), fread(), flread();
|
---|
| 16 | extern long getl();
|
---|
| 17 |
|
---|
| 18 | FILE *B_file; /* file pointer */
|
---|
| 19 |
|
---|
| 20 | struct EXFILE B_fhdr; /* executable file header */
|
---|
| 21 |
|
---|
| 22 | long B_txt_o, /* text origin from file header */
|
---|
| 23 | B_dat_o, /* data origin from file header */
|
---|
| 24 | B_bss_o, /* bss origin from file header */
|
---|
| 25 | B_txt_l, /* text length from file header */
|
---|
| 26 | B_dat_l, /* data length from file header */
|
---|
| 27 | B_bss_l, /* bss length from file header */
|
---|
| 28 | B_lod_l; /* total data length checked */
|
---|
| 29 |
|
---|
| 30 | long B_chk, /* checksum */
|
---|
| 31 | B_end; /* end address */
|
---|
| 32 |
|
---|
| 33 | /* |
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | /*
|
---|
| 37 | ============================================================================
|
---|
| 38 | check(fn) -- checksum file named by string 'fn'.
|
---|
| 39 | ============================================================================
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | check(fn)
|
---|
| 43 | char *fn;
|
---|
| 44 | {
|
---|
| 45 | register long i, sum, bgnbss, endbss, total_l;
|
---|
| 46 |
|
---|
| 47 | /* initialize the origins and lengths to 0 */
|
---|
| 48 |
|
---|
| 49 | B_txt_o = 0L;
|
---|
| 50 | B_dat_o = 0L;
|
---|
| 51 | B_bss_o = 0L;
|
---|
| 52 | B_txt_l = 0L;
|
---|
| 53 | B_dat_l = 0L;
|
---|
| 54 | B_bss_l = 0L;
|
---|
| 55 | B_lod_l = 0L;
|
---|
| 56 |
|
---|
| 57 | /* open the file */
|
---|
| 58 |
|
---|
| 59 | if (NULL EQ (B_file = fopenb(fn, "r"))) {
|
---|
| 60 |
|
---|
| 61 | printf("check: Unable to open \042%s\042\n", fn);
|
---|
| 62 | return(1);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /* read in the file header */
|
---|
| 66 |
|
---|
| 67 | if (1 NE fread(&B_fhdr, sizeof B_fhdr, 1, B_file)) {
|
---|
| 68 |
|
---|
| 69 | printf("check: Unable to read header for \042%s\042\n", fn);
|
---|
| 70 | fclose(B_file);
|
---|
| 71 | return(2);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /* check the magic */
|
---|
| 75 |
|
---|
| 76 | if ((B_fhdr.F_Magic NE F_R_C) AND (B_fhdr.F_Magic NE F_R_D)) {
|
---|
| 77 |
|
---|
| 78 | printf("check: Bad magic [$%04x] in file \042%s\042",
|
---|
| 79 | B_fhdr.F_Magic, fn);
|
---|
| 80 |
|
---|
| 81 | fclose(B_file);
|
---|
| 82 | return(3);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /* |
---|
| 86 |
|
---|
| 87 | */
|
---|
| 88 |
|
---|
| 89 | /* if it's a discontinuous file, read the origins */
|
---|
| 90 |
|
---|
| 91 | if (B_fhdr.F_Magic EQ F_R_D) {
|
---|
| 92 |
|
---|
| 93 | B_dat_o = getl(B_file);
|
---|
| 94 | B_bss_o = getl(B_file);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | B_txt_o = B_fhdr.F_Res2;
|
---|
| 98 | B_lod_l = B_fhdr.F_Text + B_fhdr.F_Data;
|
---|
| 99 | sum = 0L;
|
---|
| 100 |
|
---|
| 101 | for (i = 0; i < B_lod_l; i++) {
|
---|
| 102 |
|
---|
| 103 | sum += getc(B_file) & 0x000000FFL;
|
---|
| 104 |
|
---|
| 105 | if (ferror(B_file)) {
|
---|
| 106 |
|
---|
| 107 | printf("check: Unable to read \042%s\042\n", fn);
|
---|
| 108 | fclose(B_file);
|
---|
| 109 | return(4);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | fclose(B_file);
|
---|
| 114 |
|
---|
| 115 | B_chk = sum;
|
---|
| 116 | B_txt_l = B_fhdr.F_Text;
|
---|
| 117 | B_dat_l = B_fhdr.F_Data;
|
---|
| 118 | B_bss_l = B_fhdr.F_BSS;
|
---|
| 119 | B_end = B_txt_o + B_lod_l - 1L;
|
---|
| 120 |
|
---|
| 121 | if (B_bss_o)
|
---|
| 122 | bgnbss = B_bss_o;
|
---|
| 123 | else
|
---|
| 124 | bgnbss = B_end + 1L;
|
---|
| 125 |
|
---|
| 126 | endbss = bgnbss + B_bss_l - 1L;
|
---|
| 127 |
|
---|
| 128 | printf("File \042%s\042 is a %s file and\n",
|
---|
| 129 | fn, (B_fhdr.F_Magic EQ F_R_D) ? "Scattered" : "Contiguous");
|
---|
| 130 |
|
---|
| 131 | printf("loads from $%08lx to $%08lx (%ld bytes)\n",
|
---|
| 132 | B_txt_o, B_end, B_lod_l);
|
---|
| 133 |
|
---|
| 134 | printf(" with BSS $%08lx to $%08lx (%ld bytes)\n\n",
|
---|
| 135 | bgnbss, endbss, B_bss_l);
|
---|
| 136 |
|
---|
| 137 | printf(" B_txt_o = $%08lx, B_dat_o = $%08lx, B_bss_o = $%08lx\n",
|
---|
| 138 | B_txt_o, B_dat_o, B_bss_o);
|
---|
| 139 |
|
---|
| 140 | printf(" B_txt_l = $%08lx, B_dat_l = $%08lx, B_bss_l = $%08lx\n\n",
|
---|
| 141 | B_txt_l, B_dat_l, B_bss_l);
|
---|
| 142 |
|
---|
| 143 | total_l = B_txt_l + B_dat_l + B_bss_l;
|
---|
| 144 |
|
---|
| 145 | printf("Checksum = $%08lx, Total program length = %ld ($%08lx) bytes\n",
|
---|
| 146 | B_chk, total_l, total_l);
|
---|
| 147 |
|
---|
| 148 | return(0);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /* |
---|
| 152 |
|
---|
| 153 | */
|
---|
| 154 |
|
---|
| 155 | main(argc, argv)
|
---|
| 156 | int argc;
|
---|
| 157 | char *argv[];
|
---|
| 158 | {
|
---|
| 159 | int rc;
|
---|
| 160 |
|
---|
| 161 | if (argc NE 2) {
|
---|
| 162 |
|
---|
| 163 | printf("check: ERROR - file name required\n");
|
---|
| 164 | exit(1);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | if (rc = check(argv[1])) {
|
---|
| 168 |
|
---|
| 169 | printf("ERROR: check(\042%s\042) returned %d.\n",
|
---|
| 170 | argv[1], rc);
|
---|
| 171 | exit(1);
|
---|
| 172 |
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | exit(0);
|
---|
| 176 | }
|
---|