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