[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | blkwr.c -- write a block of 0..32767 sectors
|
---|
| 4 | Version 14 -- 1987-12-15 -- D.N. Lynx Crowe
|
---|
| 5 |
|
---|
| 6 | int
|
---|
| 7 | blkwr(fcp, buf, ns)
|
---|
| 8 | struct fcb *fcp;
|
---|
| 9 | char *buf;
|
---|
| 10 | int ns;
|
---|
| 11 |
|
---|
| 12 | Writes 'ns' sectors from file 'fcp' into 'buf'.
|
---|
| 13 | Returns the number of unwritten sectors,
|
---|
| 14 | or 0 if all were written.
|
---|
| 15 | =============================================================================
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #define DEBUGIT 0
|
---|
| 19 |
|
---|
| 20 | #include "stddefs.h"
|
---|
| 21 | #include "biosdefs.h"
|
---|
| 22 | #include "errno.h"
|
---|
| 23 | #include "errdefs.h"
|
---|
| 24 | #include "fspars.h"
|
---|
| 25 |
|
---|
[e225e77] | 26 | #include "memory.h"
|
---|
| 27 |
|
---|
[7258c6a] | 28 | extern int16_t _nsic(struct fcb *fcp, struct bpb *bpp, int8_t *fp);
|
---|
| 29 | extern int16_t _alcnew(struct fcb *fcp);
|
---|
| 30 | extern int16_t _newcls(void);
|
---|
[f40a309] | 31 |
|
---|
[e225e77] | 32 | extern int16_t micons(int16_t wi);
|
---|
| 33 | extern uint16_t _cl2lsn(struct bpb *bpp, uint16_t clnum);
|
---|
| 34 | extern void _ptcl12(uint16_t *fat, uint16_t cl, uint16_t val);
|
---|
| 35 |
|
---|
[7258c6a] | 36 | extern int32_t _berrno; /* last file system bios error number */
|
---|
[f40a309] | 37 |
|
---|
| 38 | extern struct bpb *_thebpb; /* current bios parameter block */
|
---|
| 39 |
|
---|
[7258c6a] | 40 | extern uint16_t _thefat[]; /* current file allocation table */
|
---|
[f40a309] | 41 |
|
---|
[7258c6a] | 42 | extern int16_t _fatmod; /* FAT modified flag */
|
---|
[f40a309] | 43 |
|
---|
| 44 | #if DEBUGIT
|
---|
| 45 | extern short fsdebug;
|
---|
| 46 | #endif
|
---|
| 47 |
|
---|
| 48 | #if TBUFFER
|
---|
| 49 |
|
---|
| 50 | /* WARNING: this ONLY works for 512 byte sectors, 9 sectors per track */
|
---|
| 51 |
|
---|
[7258c6a] | 52 | extern int16_t _b_tbuf[9][256]; /* the track buffer */
|
---|
[f40a309] | 53 |
|
---|
[7258c6a] | 54 | extern int16_t _b_trak; /* current track */
|
---|
| 55 | extern int16_t _b_side; /* current side */
|
---|
| 56 | extern int16_t _b_sect; /* current sector */
|
---|
| 57 | extern int16_t _b_tsec; /* base sector for current track */
|
---|
[f40a309] | 58 |
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
| 61 | /* |
---|
| 62 |
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 | #if TBUFFER
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
| 68 | =============================================================================
|
---|
| 69 | _secwr(buf, rec) -- write a logical sector into the track buffer
|
---|
| 70 | =============================================================================
|
---|
| 71 | */
|
---|
[7258c6a] | 72 |
|
---|
[f40a309] | 73 | int32_t _secwr(int8_t *buf, int16_t rec)
|
---|
[7258c6a] | 74 | {
|
---|
[f40a309] | 75 | register int16_t track, side, sector;
|
---|
| 76 |
|
---|
| 77 | if (_thebpb->dspt NE 9) /* make sure we can do this */
|
---|
| 78 | return(ERR07);
|
---|
| 79 |
|
---|
| 80 | if (_thebpb->recsiz NE 512)
|
---|
| 81 | return(ERR07);
|
---|
| 82 |
|
---|
| 83 | track = rec / _thebpb->dspc; /* desired track */
|
---|
| 84 | sector = rec - (track * _thebpb->dspc); /* logical sector */
|
---|
| 85 |
|
---|
| 86 | if (sector GE _thebpb->dspt) { /* adjust sector and side */
|
---|
| 87 |
|
---|
| 88 | sector -= _thebpb->dspt;
|
---|
| 89 | side = 1;
|
---|
| 90 |
|
---|
| 91 | } else {
|
---|
| 92 |
|
---|
| 93 | side = 0;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #if DEBUGIT
|
---|
| 97 | if (fsdebug) {
|
---|
| 98 |
|
---|
| 99 | printf("_secwr($%08.8LX, %d): track=%d, side=%d, sector=%d\n",
|
---|
| 100 | buf, rec, track, side, sector);
|
---|
| 101 | printf("_secwr(): _b_trak=%d, _b_side=%d, _b_sect=%d, _b_tsec=%d\n",
|
---|
| 102 | _b_trak, _b_side, _b_sect, _b_tsec);
|
---|
| 103 | }
|
---|
| 104 | #endif
|
---|
| 105 |
|
---|
| 106 | if ((track NE _b_trak) OR (side NE _b_side)) /* track in buffer ? */
|
---|
| 107 | return(0L);
|
---|
[7258c6a] | 108 |
|
---|
[f40a309] | 109 | memcpy((int8_t *)_b_tbuf[sector], buf, 512); /* update the buffer */
|
---|
| 110 |
|
---|
| 111 | #if DEBUGIT
|
---|
| 112 | if (fsdebug)
|
---|
| 113 | printf("_secwr(): updated track buffer\n");
|
---|
| 114 | #endif
|
---|
| 115 |
|
---|
| 116 | return(0L);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | /* |
---|
| 122 |
|
---|
| 123 | */
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
| 126 | =============================================================================
|
---|
| 127 | blkwr(fcp, buf, ns) -- write 'ns' sectors from file 'fcp' into 'buf'.
|
---|
| 128 | Returns the number of unwritten sectors, or 0 if all were written.
|
---|
| 129 | =============================================================================
|
---|
[7258c6a] | 130 | */
|
---|
[f40a309] | 131 |
|
---|
[7258c6a] | 132 | int16_t blkwr(struct fcb *fcp, int8_t *buf, int16_t ns)
|
---|
| 133 | {
|
---|
[f40a309] | 134 | int32_t rc;
|
---|
| 135 | int16_t clustr;
|
---|
| 136 |
|
---|
| 137 | while (ns > 0) { /* write a sector at a time */
|
---|
| 138 |
|
---|
| 139 | if (fcp->asects) {
|
---|
| 140 |
|
---|
| 141 | if (fcp->curlsn LT fcp->asects) {
|
---|
| 142 |
|
---|
| 143 | #if DEBUGIT
|
---|
| 144 | if (fsdebug)
|
---|
| 145 | printf("blkwr(): [1] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
| 146 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
[7258c6a] | 147 | #endif
|
---|
[f40a309] | 148 |
|
---|
| 149 | if (rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0)) {
|
---|
| 150 |
|
---|
| 151 | #if DEBUGIT
|
---|
| 152 | if (fsdebug)
|
---|
| 153 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 154 | #endif
|
---|
| 155 |
|
---|
| 156 | _berrno = rc; /* log the error */
|
---|
| 157 | errno = EIO;
|
---|
| 158 | return(ns); /* return unwritten sector count */
|
---|
| 159 | }
|
---|
[7258c6a] | 160 |
|
---|
[f40a309] | 161 | #if TBUFFER
|
---|
| 162 | _secwr(buf, (int16_t)fcp->curdsn);
|
---|
| 163 | #endif
|
---|
| 164 |
|
---|
| 165 | rc = _nsic(fcp, _thebpb, _thefat);
|
---|
| 166 |
|
---|
| 167 | #if DEBUGIT
|
---|
| 168 | if (fsdebug)
|
---|
| 169 | printf("blkwr(): _nsic() rc=%ld\n", rc);
|
---|
| 170 | #endif
|
---|
| 171 |
|
---|
| 172 | if (--ns EQ 0) /* done if no more to do */
|
---|
| 173 | return(0);
|
---|
| 174 |
|
---|
| 175 | if (rc < 0) { /* bad cluster ? */
|
---|
| 176 |
|
---|
| 177 | errno = EIO; /* set error number */
|
---|
| 178 | return(ns); /* return unwritten sector count */
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | buf += _thebpb->recsiz;
|
---|
| 182 | /* |
---|
| 183 |
|
---|
| 184 | */
|
---|
| 185 | } else if (fcp->curlsn EQ fcp->asects) {
|
---|
| 186 |
|
---|
| 187 | if (_alcnew(fcp)) { /* allocate a cluster */
|
---|
| 188 |
|
---|
| 189 | errno = EIO; /* set error number */
|
---|
| 190 | return(ns); /* return unwritten sector count */
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | fcp->modefl &= ~FC_EOF; /* clear EOF flag */
|
---|
| 194 |
|
---|
| 195 | #if DEBUGIT
|
---|
| 196 | if (fsdebug)
|
---|
| 197 | printf("blkwr(): [2] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
[7258c6a] | 198 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
[f40a309] | 199 | #endif
|
---|
| 200 |
|
---|
| 201 | if (rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0)) {
|
---|
| 202 |
|
---|
| 203 | #if DEBUGIT
|
---|
| 204 | if (fsdebug)
|
---|
| 205 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 206 | #endif
|
---|
| 207 |
|
---|
| 208 | _berrno = rc; /* log the error */
|
---|
| 209 | errno = EIO;
|
---|
| 210 | return(ns); /* return unwritten sector count */
|
---|
[7258c6a] | 211 | }
|
---|
[f40a309] | 212 |
|
---|
| 213 | #if TBUFFER
|
---|
| 214 | _secwr(buf, (int16_t)fcp->curdsn);
|
---|
| 215 | #endif
|
---|
| 216 |
|
---|
| 217 | ++fcp->curlsn;
|
---|
| 218 | ++fcp->clsec;
|
---|
| 219 | ++fcp->curdsn;
|
---|
| 220 |
|
---|
| 221 | if (--ns EQ 0)
|
---|
| 222 | return(0);
|
---|
| 223 |
|
---|
| 224 | buf += _thebpb->recsiz; /* advance buffer pointer */
|
---|
| 225 | }
|
---|
| 226 | /* |
---|
| 227 |
|
---|
| 228 | */
|
---|
| 229 | } else {
|
---|
| 230 |
|
---|
| 231 | if (0 EQ (clustr = _newcls())) {
|
---|
| 232 |
|
---|
| 233 | errno = EIO;
|
---|
| 234 | return(ns);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | fcp->de.bclust = micons(clustr);
|
---|
| 238 | _ptcl12(_thefat, clustr, 0x0FF8);
|
---|
| 239 | _fatmod = TRUE;
|
---|
| 240 | fcp->curdsn = _cl2lsn(_thebpb, clustr);
|
---|
| 241 | fcp->curcls = clustr;
|
---|
| 242 | fcp->clsec = 0;
|
---|
| 243 | fcp->asects = _thebpb->clsiz;
|
---|
| 244 | fcp->modefl &= ~FC_EOF;
|
---|
| 245 |
|
---|
| 246 | fcp->modefl &= ~FC_EOF; /* clear EOF flag */
|
---|
| 247 |
|
---|
| 248 | #if DEBUGIT
|
---|
| 249 | if (fsdebug)
|
---|
[7258c6a] | 250 | printf("blkwr(): [3] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
[f40a309] | 251 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
| 252 | #endif
|
---|
| 253 |
|
---|
| 254 | if (rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0)) {
|
---|
| 255 |
|
---|
| 256 | #if DEBUGIT
|
---|
| 257 | if (fsdebug)
|
---|
| 258 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 259 | #endif
|
---|
| 260 |
|
---|
| 261 | _berrno = rc; /* log the error */
|
---|
| 262 | errno = EIO;
|
---|
[7258c6a] | 263 | return(ns); /* return unwritten sector count */
|
---|
[f40a309] | 264 | }
|
---|
| 265 |
|
---|
| 266 | #if TBUFFER
|
---|
| 267 | _secwr(buf, (int16_t)fcp->curdsn);
|
---|
| 268 | #endif
|
---|
| 269 |
|
---|
| 270 | ++fcp->curlsn;
|
---|
| 271 | ++fcp->clsec;
|
---|
| 272 | ++fcp->curdsn;
|
---|
| 273 |
|
---|
| 274 | if (--ns EQ 0)
|
---|
| 275 | return(0);
|
---|
| 276 |
|
---|
| 277 | buf += _thebpb->recsiz; /* advance buffer pointer */
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | return(ns); /* return (will be zero or negative) */
|
---|
| 282 | }
|
---|