const UCHAR *cmd_ovrcreate(const char *cmd) { int i, asiz = -1; const UCHAR *p; cmd += *cmd + 2; if (optmatch(cmd, "file") == 0) errend(20); /* ovrcreate command error */ p = cmd + (4 + 2); cmd += *cmd + 2; if (optmatch(cmd, "size") == 0) errend(20); /* ovrcreate command error */ len_filebuf = getnum(cmd + (4 + 2)); for (i = 0; (unsigned int) i < len_filebuf; i++) filebuf0[i] = 0x00; if (len_filebuf >= SIZ_FILEBUF) errend(20); /* ovrcreate command error */ cmd += *cmd + 2; i = 2; optloop: if (optmatch(cmd, "asiz")) { asiz = (getnum(cmd + (4 + 2)) + len_clu - 1) / len_clu; cmd += *cmd + 2; goto optloop; } if (optmatch(cmd, "begin")) { i = getnum(cmd + (5 + 2)); cmd += *cmd + 2; goto optloop; } ovrwritefile(pathfix(p, 2), i, asiz, 0x00); return cmd; }
const UCHAR *cmd_release(const char *cmd) { unsigned int i, j; UCHAR *p; if ((flag & 0x03) == 0) { errend(26); /* release command error */ } optloop: cmd += *cmd + 2; if (optmatch(cmd, "mini")) { while (limit_fat > 2 && fat[limit_fat - 1] == 0) limit_fat--; len_imgbuf = clu0002 + (limit_fat - 2) * len_clu - imgbuf0; goto optloop; } if (optmatch(cmd, "zerofill")) { p = clu0002; for (j = 2; j < limit_fat; j++, p += len_clu) { if (fat[j] == 0) { for (i = 0; i < len_clu; i++) p[i] = 0x00; } } goto optloop; } if (optmatch(cmd, "efat")) { if ((flag & 0x04) == 0) errend(32); fat[0] ^= 0x0007; for (i = 2; i < 0xfff6; i++) { if (fat[i] == 0xfffe) errend(33); if (fat[i] == i + 1) fat[i] = 0xfffe; } BPB[0x47] |= 0x10; goto optloop; } if (optmatch(cmd, "nofrag")) { /* たぶんefatのあとに実行しても正しく判定できる */ if ((flag & 0x04) == 0) errend(33); for (i = 2; i < limit_fat; i++) { if (2 <= fat[i] && fat[i] < 0xfff7 && fat[i] != i + 1) errend(35); } BPB[0x47] |= 0x20; goto optloop; } if (optmatch(cmd, "minibpb")) { while (limit_fat > 2 && fat[limit_fat - 1] == 0) limit_fat--; len_imgbuf = clu0002 + (limit_fat - 2) * len_clu - imgbuf0; bpbfix_sub(); goto optloop; } return cmd; }
const UCHAR *cmd_setattr(const char *cmd) { const UCHAR *p; UCHAR attr; cmd += *cmd + 2; if (optmatch(cmd, "file") == 0) errend(22); /* setattr command error */ p = cmd + (4 + 2); cmd += *cmd + 2; if (optmatch(cmd, "attr") == 0) errend(22); /* setattr command error */ attr = getnum(cmd + (4 + 2)); setattrfile(pathfix(p, 2), attr); return cmd + (*cmd + 2); }
const UCHAR *cmd_wbinimg(const char *cmd) { unsigned int len, from, to, i; UCHAR *p, *q; cmd += *cmd + 2; if ((flag & 0x13) == 0) { err: errend(25); /* wbinimg command error */ } if (optmatch(cmd, "src") == 0) goto err; readfile(pathfix(cmd + (3 + 2), 2), 0); cmd += *cmd + 2; if (optmatch(cmd, "len") == 0) goto err; len = getnum(cmd + (3 + 2)); cmd += *cmd + 2; if (optmatch(cmd, "from") == 0) goto err; from = getnum(cmd + (4 + 2)); cmd += *cmd + 2; if (optmatch(cmd, "to") == 0) goto err; to = getnum(cmd + (2 + 2)); p = filebuf0 + from; q = BPB + to; for (i = 0; i < len; i++) q[i] = p[i]; return cmd + (*cmd + 2); }
const UCHAR *cmd_list(const char *cmd) { UCHAR s[68]; const UCHAR *p = dir0; unsigned int i; if ((flag & 0x03) == 0) errend(31); /* list command error */ do { if (*p == '\0') break; if (*p != 0xe5 && (p[11] & 0x08) == 0) { *(int *) &s[0] = *(int *) &p[0]; *(int *) &s[4] = *(int *) &p[4]; *(int *) &s[8] = (*(int *) &p[8]) << 8 | ' '; i = 0x20202020; *(int *) &s[16] = *(int *) &s[12] = i; *(int *) &s[32] = *(int *) &s[28] = i; *(int *) &s[40] = *(int *) &s[36] = i; *(int *) &s[48] = *(int *) &s[44] = i; *(int *) &s[56] = *(int *) &s[52] = i; *(int *) &s[64] = 0x000a2020; *(int *) &s[24] = *(int *) &s[20] = 0x2d2d2d2d; s[19] = '['; s[28] = ']'; if (p[11] & 0x10) { *(int *) &s[12] = 0x49443c20; *(short *) &s[12] = 0x3e52; s[23] = 'd'; } if (p[11] & 0x80) s[20] = '?'; if (p[11] & 0x40) s[21] = '?'; if (p[11] & 0x20) s[22] = 'a'; if (p[11] & 0x04) s[25] = 's'; if (p[11] & 0x02) s[26] = 'h'; if (p[11] & 0x01) s[27] = 'r'; setdec(&s[38], *(int *) &p[28], 9); if ((i = *(unsigned short *) &p[24]) != 0) { setdec(&s[42], (i >> 9) + 80, 2); s[43] = '-'; setdec(&s[45], ((i >> 5) & 0x0f) + 100, 2); s[46] = '-'; setdec(&s[48], (i & 0x1f) + 100, 2); i = *(unsigned short *) &p[22]; setdec(&s[51], i >> 11, 2); s[52] = ':'; setdec(&s[54], ((i >> 5) & 0x3f) + 100, 2); s[55] = ':'; setdec(&s[57], ((i & 0x1f) << 1) + 100, 2); } s[60] = '#'; setdec(&s[65], 100000 + *(unsigned short *) &p[26], 5); fputs(s, stdout); }
const UCHAR *cmd_delete(const char *cmd) { cmd += *cmd + 2; if (optmatch(cmd, "file") == 0) errend(21); /* delete command error */ deletefile(pathfix(cmd + (4 + 2), 2)); return cmd + (*cmd + 2); }
const UCHAR *cmd_ovrcopy(const char *cmd) { char nocmp = 0; cmd += *cmd + 2; if (optmatch(cmd, "nocmp")) { cmd += *cmd + 2; nocmp = 1; } if (optmatch(cmd, "from") == 0) errend(11); /* ovrcopy command error */ readfile(pathfix(cmd + (4 + 2), 0), 0x0f); cmd += *cmd + 2; if (nocmp) len_filebuf = autodecomp2(SIZ_FILEBUF, filebuf0, len_filebuf); if (optmatch(cmd, "to") == 0) errend(11); /* ovrcopy command error */ ovrwritefile(pathfix(cmd + (2 + 2), 1), 2, -1, 0x04); /* 更新日のみ更新 */ return cmd + (*cmd + 2); }
const UCHAR *cmd_writedata(const char *cmd) { unsigned int ofs, data, rep; const UCHAR *p; UCHAR datasize, tmp; if ((flag & 0x13) == 0) { err: errend(30); /* writedata command error */ } cmd += *cmd + 2; if (optmatch(cmd, "offset") == 0) goto err; ofs = getnum(cmd + (6 + 2)); rep = 1; for (;;) { cmd += *cmd + 2; if (optmatch(cmd, "repeat")) { rep *= getnum(cmd + (6 + 2)); continue; } datasize = 0; if (optmatch(cmd, "byte")) datasize = 1; if (optmatch(cmd, "word")) datasize = 2; if (optmatch(cmd, "dwrd")) datasize = 4; if (datasize == 0) break; if (rep) { do { p = cmd + (4 + 2); do { data = getnum(p); while (*p != ',' && *p != '\0') p++; p++; tmp = datasize; do { BPB[ofs++] = data & 0xff; data >>= 8; } while (--tmp != 0); } while (p[-1] == ','); } while (--rep); } rep = 1; } return cmd; }
int readcfg(void) { char ports[BUFSIZ], *p; int port; pipe_s *pipe; cfg_s local; serialinfo_s sinfo; char *parity; /* Read the global config settings */ cfg_fromfile(&cfg, cfgfile); /* Read the comm port list */ if (cfg_readbuf(cfgfile, "comm_ports", ports, sizeof(ports)) == NULL) errend("Couldn't find 'comm_ports' entry in config file '%s'", cfgfile); vlist_clear(&pipes); /* Parse the comm ports list */ p = strtok(ports, ","); while (p) { if (sscanf(p, "%d", &port) > 0) { pipe = malloc(sizeof(pipe_s)); //pipe_init(pipe); if (pipe == NULL) perrend("malloc(pipe_s)"); cfg_init(&local, port); /* Copy global settings to those for current pipe */ cfg_assign(&local, &cfg); /* Set the comm port */ local.ints[CFG_IPORT].val = port; /* Load this pipe's config */ cfg_fromfile(&local, cfgfile); /* Try initializing the pipe */ if (pipe_init(pipe, local.ints[CFG_INETPORT].val)) perrend("pipe_init"); /* Copy over the rest of the pipe's config */ pipe->timeout = local.ints[CFG_ITIMEOUT].val; sinfo.port = port; sinfo.baud = local.ints[CFG_IBAUD].val; sinfo.stopbits = local.ints[CFG_ISTOP].val; sinfo.databits = local.ints[CFG_IDATA].val; parity = local.strs[CFG_SPARITY].val; if (strcmp(parity, "none") == 0) { sinfo.parity = SIO_PARITY_NONE; } else if (strcmp(parity, "even") == 0) { sinfo.parity = SIO_PARITY_EVEN; } else if (strcmp(parity, "odd") == 0) { sinfo.parity = SIO_PARITY_ODD; } else { errend("Unknown parity string '%s'", parity); } if (sio_setinfo(&pipe->sio, &sinfo)) errend("Unable to configure comm port %d", port); /* Finally add the pipe to the pipes list */ vlist_add(&pipes, pipes.tail, pipe); cfg_cleanup(&local); } p = strtok(NULL, ","); } /* Clean up local cfg struct */ cfg_cleanup(&local); return 0; }
int main(int argc, UCHAR **argv) { UCHAR *script0 = malloc(SIZ_SCRIPT); // fat_flags = malloc(65536); fat = malloc(65536 * 2); filebuf0 = malloc(SIZ_FILEBUF); imgbuf0 = malloc(SIZ_IMGBUF); UCHAR *p, *q, *r, *script1; int i; FILE *fp; if (imgbuf0 == NULL) errend(1); /* out of memory */ /* スクリプト準備 */ q = script0; while ((p = *++argv) != NULL) { if (*p != '@') { r = ++q; while ((*q++ = *p++) != '\0') { if (q - script0 >= SIZ_SCRIPT - 4) errend(2); /* script too long */ } if (q - r > 255 + 1) errend(3); /* script too long */ *(r - 1) = (q - r) - 1; continue; } p++; fp = fopen(p, "rb"); if (fp == NULL) errend(4); /* script file open error */ i = fread(filebuf0, 1, SIZ_FILEBUF, fp); if (i >= SIZ_FILEBUF) errend(5); /* script file open error */ script1 = filebuf0 + i; p = filebuf0; for (;;) { while (p < script1 && *p <= ' ') p++; if (p >= script1) break; r = ++q; while (p < script1 && *p > ' ') { if (q - script0 >= SIZ_SCRIPT - 4) errend(6); /* script too long */ *q++ = *p++; } if (q - r > 255) errend(7); /* script too long */ *(r - 1) = q - r; *q++ = '\0'; } } *q++ = '\0'; script1 = q; /* スクリプト解釈 */ p = script0; for (;;) { if (*p == '\0') break; if (cmdmatch(p, "copy")) { p = (UCHAR *) cmd_copy(p); continue; } if (cmdmatch(p, "ovrcopy")) { p = (UCHAR *) cmd_ovrcopy(p); continue; } if (cmdmatch(p, "create")) { p = (UCHAR *) cmd_create(p); continue; } if (cmdmatch(p, "ovrcreate")) { p = (UCHAR *) cmd_ovrcreate(p); continue; } if (cmdmatch(p, "delete")) { p = (UCHAR *) cmd_delete(p); continue; } if (cmdmatch(p, "setattr")) { p = (UCHAR *) cmd_setattr(p); continue; } if (cmdmatch(p, "wbinimg")) { p = (UCHAR *) cmd_wbinimg(p); continue; } if (cmdmatch(p, "release")) { p = (UCHAR *) cmd_release(p); continue; } if (cmdmatch(p, "writedata")) { p = (UCHAR *) cmd_writedata(p); continue; } if (cmdmatch(p, "list")) { p = (UCHAR *) cmd_list(p); continue; } if (cmdmatch(p, "copyall")) { p = (UCHAR *) cmd_copyall(p); continue; } if (cmdmatch(p, "exe2bin")) { p = (UCHAR *) cmd_exe2bin(p); continue; } if (cmdmatch(p, "opt")) { p += *p + 2; continue; } if (optmatch(p, "imgin")) { p = (UCHAR *) opt_imgin(p); continue; } if (optmatch(p, "vsiz")) { p = (UCHAR *) opt_vsiz(p); continue; } if (optmatch(p, "imgout")) { p = (UCHAR *) opt_imgout(p); continue; } if (optmatch(p, "binin")) { p = (UCHAR *) opt_binin(p); continue; } if (optmatch(p, "binout")) { p = (UCHAR *) opt_binout(p); continue; } if (optmatch(p, "_path")) { p = (UCHAR *) opt_bpath(p); continue; } if (optmatch(p, "bias")) { p = (UCHAR *) opt_bias(p); continue; } if (cmdmatch(p, "/*")) { i = 1; do { p += *p + 2; if (*p == '\0') errend(9); /* comment nesting error */ if (cmdmatch(p, "/*")) i++; if (cmdmatch(p, "*/")) i--; } while (i > 0); p += *p + 2; continue; } fputs("script syntax error. : ", stderr); fputs(p + 1, stderr); errend(8); /* script syntax error */ } return 0; }
const UCHAR *pathfix(const UCHAR *path, char flg) { static UCHAR pathbuf[1024], pathbuf2[1024]; static const UCHAR *defname; UCHAR *p; const UCHAR *q, *r; if (path[0] == '_' && path[1] == ':') { p = pathbuf2; r = _path; while ((*p = *r) != '\0') { p++; r++; if ((unsigned int) (p - pathbuf2) >= (sizeof pathbuf) - 1) goto toolong; } r = path + 2; while ((*p = *r) != '\0') { p++; r++; if ((unsigned int) (p - pathbuf2) >= (sizeof pathbuf) - 1) goto toolong; } path = pathbuf2; } if (flg == 1) { p = (UCHAR *) path; while (*p != '\0') p++; if (p - path >= 1) { if (p[-1] == '/') goto fix; if (p[-1] == '\\') goto fix; if (p[-1] == ':') { fix: if ((unsigned int) (p - path) >= sizeof pathbuf) { toolong: fputs("too long path. : ", stderr); fputs(path, stderr); errend(12); } p = pathbuf; do { *p++ = *path++; } while (*path != '\0'); path = defname; while (*path != '\0') { if ((unsigned int) (p - pathbuf) >= (sizeof pathbuf) - 1) { fputs("too long path. : ", stderr); fputs(pathbuf, stderr); errend(12); } *p++ = *path++; } *p = '\0'; path = pathbuf; } } } if (flg == 0) { q = path; r = path; if (*q != '\0') { do { if (*q == ':') r = q + 1; if (*q == '/') r = q + 1; if (*q == '\\') r = q + 1; q++; } while (*q != '\0'); } defname = r; } return path; }