[f40a309] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | booter.c -- load an absolute format Alcyon object file
|
---|
| 4 | Version 18 -- 1987-11-06 -- D.N. Lynx Crowe
|
---|
| 5 | ============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define PRINTIT 1 /* define non-zero to get printf output from booter */
|
---|
| 9 |
|
---|
[b28a12e] | 10 | #include "rom.h"
|
---|
[f40a309] | 11 |
|
---|
| 12 | static FILE *B_file; /* boot file pointer */
|
---|
| 13 |
|
---|
| 14 | struct EXFILE B_fhdr; /* executable file header */
|
---|
| 15 |
|
---|
[7258c6a] | 16 | int32_t B_txt_o, /* test origin from file header */
|
---|
[f40a309] | 17 | B_dat_o, /* data origin from file header */
|
---|
| 18 | B_bss_o, /* bss origin from file header */
|
---|
| 19 | B_txt_l, /* text length from file header */
|
---|
| 20 | B_dat_l, /* data length from file header */
|
---|
| 21 | B_bss_l, /* bss length from file header */
|
---|
| 22 | B_lod_l, /* total data length loaded */
|
---|
| 23 | B_end, /* end address */
|
---|
| 24 | B_chk; /* checksum */
|
---|
| 25 |
|
---|
[7258c6a] | 26 | int8_t *B_buf_a; /* boot load address */
|
---|
[f40a309] | 27 |
|
---|
[7258c6a] | 28 | int16_t B_log_s; /* boot log switch */
|
---|
| 29 | int16_t B_dbg_s; /* boot debug switch */
|
---|
[f40a309] | 30 |
|
---|
| 31 | /* |
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /*
|
---|
| 35 | ============================================================================
|
---|
| 36 | booter(fn, textadr) -- load file named by string 'fn' at 'textadr'.
|
---|
| 37 | If 'textadr' is 0, the text origin from the file will be used.
|
---|
| 38 | Returns 0 if load was OK, non-zero error code otherwise.
|
---|
| 39 | ============================================================================
|
---|
| 40 | */
|
---|
[7258c6a] | 41 |
|
---|
[f40a309] | 42 | int16_t booter(int8_t *fn, int32_t textadr)
|
---|
[7258c6a] | 43 | {
|
---|
| 44 | register int32_t i, bgnbss, endbss;
|
---|
[f40a309] | 45 | register int8_t *cp;
|
---|
| 46 | #if PRINTIT
|
---|
| 47 | register struct fcb *fcp;
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 | /* initialize the origins and lengths to 0 */
|
---|
| 51 |
|
---|
| 52 | B_txt_o = 0L;
|
---|
| 53 | B_dat_o = 0L;
|
---|
| 54 | B_bss_o = 0L;
|
---|
| 55 | B_txt_l = 0L;
|
---|
| 56 | B_dat_l = 0L;
|
---|
| 57 | B_bss_l = 0L;
|
---|
| 58 | B_lod_l = 0L;
|
---|
| 59 |
|
---|
| 60 | /* open the file */
|
---|
| 61 |
|
---|
| 62 | if (NULL EQ (B_file = fopenb(fn, "r"))) {
|
---|
| 63 |
|
---|
| 64 | #if PRINTIT
|
---|
| 65 | if (B_log_s)
|
---|
| 66 | printf("booter: Unable to open \042%s\042\n", fn);
|
---|
| 67 | #endif
|
---|
| 68 | return(1);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | #if PRINTIT
|
---|
| 72 | if (B_dbg_s) { /* if we're debugging, print the FCB stuff */
|
---|
| 73 |
|
---|
| 74 | fcp = (struct fcb *)(chantab[B_file->_unit].c_arg);
|
---|
| 75 |
|
---|
| 76 | SnapFCB(fcp);
|
---|
[6262b5c] | 77 | ClusMap(fcp);
|
---|
[f40a309] | 78 | waitcr2();
|
---|
| 79 | }
|
---|
| 80 | #endif
|
---|
| 81 |
|
---|
| 82 | /* read in the file header */
|
---|
| 83 |
|
---|
| 84 | if (1 NE fread(&B_fhdr, sizeof B_fhdr, 1, B_file)) {
|
---|
| 85 |
|
---|
| 86 | #if PRINTIT
|
---|
| 87 | if (B_log_s)
|
---|
| 88 | printf("booter: Unable to read header for \042%s\042\n", fn);
|
---|
| 89 | #endif
|
---|
| 90 | fclose(B_file);
|
---|
| 91 | return(2);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /* check the magic */
|
---|
| 95 |
|
---|
| 96 | if ((B_fhdr.F_Magic NE F_R_C) AND (B_fhdr.F_Magic NE F_R_D)) {
|
---|
| 97 |
|
---|
| 98 | #if PRINTIT
|
---|
| 99 | if (B_log_s)
|
---|
| 100 | printf("booter: Bad magic [0x%04x] in file \042%s\042",
|
---|
| 101 | B_fhdr.F_Magic, fn);
|
---|
| 102 | #endif
|
---|
| 103 | fclose(B_file);
|
---|
| 104 | return(3);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* |
---|
| 108 |
|
---|
| 109 | */
|
---|
| 110 |
|
---|
| 111 | /* if it's a discontinuous file, read the origins */
|
---|
| 112 |
|
---|
| 113 | if (B_fhdr.F_Magic EQ F_R_D) {
|
---|
| 114 |
|
---|
| 115 | B_dat_o = getl(B_file);
|
---|
| 116 | B_bss_o = getl(B_file);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | B_txt_o = B_fhdr.F_Res2;
|
---|
| 120 |
|
---|
| 121 | B_buf_a = textadr ? textadr : B_txt_o;
|
---|
| 122 | B_lod_l = B_fhdr.F_Text + B_fhdr.F_Data;
|
---|
| 123 |
|
---|
| 124 | if (0 NE flread(B_buf_a, B_lod_l, B_file)) {
|
---|
| 125 |
|
---|
| 126 | #if PRINTIT
|
---|
| 127 | if (B_log_s)
|
---|
| 128 | printf("booter: Unable to read \042%s\042\n", fn);
|
---|
| 129 | #endif
|
---|
| 130 | fclose(B_file);
|
---|
| 131 | return(4);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | B_end = B_buf_a + B_lod_l - 1L;
|
---|
| 135 |
|
---|
| 136 | B_txt_l = B_fhdr.F_Text;
|
---|
| 137 | B_dat_l = B_fhdr.F_Data;
|
---|
| 138 | B_bss_l = B_fhdr.F_BSS;
|
---|
| 139 |
|
---|
| 140 | cp = B_buf_a; /* calculate checksum */
|
---|
| 141 | B_chk = 0L;
|
---|
| 142 |
|
---|
| 143 | for (i = 0; i < B_lod_l; i++)
|
---|
| 144 | B_chk += *cp++ & 0x000000FFL;
|
---|
| 145 |
|
---|
| 146 | if (B_bss_o)
|
---|
| 147 | bgnbss = B_bss_o;
|
---|
| 148 | else
|
---|
| 149 | bgnbss = B_end + 1L;
|
---|
| 150 |
|
---|
| 151 | endbss = bgnbss + B_bss_l - 1L;
|
---|
| 152 |
|
---|
| 153 | #if PRINTIT
|
---|
| 154 | if (B_log_s) {
|
---|
| 155 |
|
---|
| 156 | printf("File \042%s\042 loaded from $%08lX to $%08lX\r\n",
|
---|
| 157 | fn, B_buf_a, B_end);
|
---|
| 158 | printf(" BSS $%08lX to $%08lX\r\n", bgnbss, endbss);
|
---|
| 159 | printf("Checksum = $%08lX, Load length = %ld ($%08lX)\r\n",
|
---|
| 160 | B_chk, B_lod_l, B_lod_l);
|
---|
| 161 | printf(" B_txt_o = $%08lX, B_dat_o = $%08lX, B_bss_o = $%08lX\r\n",
|
---|
| 162 | B_txt_o, B_dat_o, B_bss_o);
|
---|
| 163 | printf(" B_txt_l = $%08lX, B_dat_l = $%08lX, B_bss_l = $%08lX\r\n",
|
---|
| 164 | B_txt_l, B_dat_l, B_bss_l);
|
---|
| 165 | }
|
---|
| 166 | #endif
|
---|
| 167 |
|
---|
| 168 | fclose(B_file);
|
---|
[6262b5c] | 169 | return(0);
|
---|
| 170 | }
|
---|
| 171 |
|
---|