From 4122fe90c2fffbec62a431ac15fb4b373aecb1ea Mon Sep 17 00:00:00 2001 From: Leo C Date: Mon, 30 May 2016 23:58:57 +0200 Subject: [PATCH] attach/detach working (minus options) --- avr/cmd_attach.c | 35 +++++++++++++------------ avr/z180-serv.c | 64 ++++++++++++--------------------------------- include/z180-serv.h | 6 ++++- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/avr/cmd_attach.c b/avr/cmd_attach.c index e1a3b2c..9fbfa1e 100644 --- a/avr/cmd_attach.c +++ b/avr/cmd_attach.c @@ -5,7 +5,7 @@ */ /* - * attach chanels to devices + * attach channels to devices */ #include "common.h" @@ -15,11 +15,6 @@ #include "command.h" #include "z180-serv.h" #include "getopt-min.h" -//#include "print-utils.h" -//#include "con-utils.h" -//#include "timer.h" -//#include "z80-if.h" -//#include "debug.h" static const FLASH char * const FLASH rc_messages[] = { @@ -68,17 +63,17 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ uint8_t unit; char *filename = NULL; bool detach = false; + bool detach_all = false; drv_opt_t options = 0; int res; (void) cmdtp; (void) flag; - if (argv[0][0] == 'd') + if (argv[0][0] == 'd') { /* we are called as 'detach' */ detach = true; - - if (argc == 1) { + } else if (argc == 1) { /* no arguments */ drv_list(); return CMD_RET_SUCCESS; @@ -88,11 +83,14 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ optind = 1; int opt; - while ((opt = getopt(argc, argv, PSTR("drwo:"))) != -1) { + while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) { switch (opt) { case 'd': detach = true; break; + case 'a': + detach_all = true; + break; case 'r': options |= DRV_OPT_RO; break; @@ -128,21 +126,26 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ /* remaining arguments */ argc -= optind; - if ( !((detach && argc == 1) || - ((options & DRV_OPT_REATTATCH) && argc == 1) || - argc == 2) ) + if ( !( (argc == 0 && detach && detach_all) || + (argc == 1 && detach) || + (argc == 1 && (options & DRV_OPT_REATTATCH)) || + argc == 2) ) return CMD_RET_USAGE; - if ((strlen(argv[optind]) != 4) || + if (argc > 0 && ((strlen(argv[optind]) != 4) || strncmp_P(argv[optind], PSTR("dsk"), 3) || - (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE) { + (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE)) { printf_P(PSTR("Unknown device: '%s'\n"), argv[optind]); return CMD_RET_FAILURE; } if (detach) { - res = drv_detach(unit); + if (detach_all) + for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) + drv_detach(i); + else + drv_detach(unit); return CMD_RET_SUCCESS; } diff --git a/avr/z180-serv.c b/avr/z180-serv.c index 1bf76b4..ddb4622 100644 --- a/avr/z180-serv.c +++ b/avr/z180-serv.c @@ -235,9 +235,9 @@ int drv_detach(uint8_t drv) if (p->img_name) { f_close(&p->fd); - p->opt = 0; - p->dirty = false; free(p->img_name); + p->opt = 0; + p->flags &= ~DRV_FLG_DIRTY; p->img_name = NULL; } } @@ -284,7 +284,7 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options) if (drv_find_file_attached(filename) >= 0) return AT_OTHER; - p->opt = options & ~DRV_OPT_REATTATCH; + p->opt = options; /* new attachment */ @@ -307,7 +307,7 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options) if (res || bw < CONFIG_CPM_BLOCK_SIZE) { debug_cpmsd(" failed! res: %d, bytes written: %u\n", res, bw); } - p->dirty = true; + p->flags |= DRV_FLG_DIRTY; bg_setstat(handle_cpm_drv_to, 1); } } @@ -337,9 +337,9 @@ int cpm_drv_to(int state) case 2: if (get_timer(ts) > 1000) { for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) { - if (drv_table[i].dirty) { + if (drv_table[i].flags & DRV_FLG_DIRTY) { f_sync(&drv_table[i].fd); - drv_table[i].dirty = false; + drv_table[i].flags &= ~DRV_FLG_DIRTY; debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i + CONFIG_CPM_BASE_DRIVE); } } @@ -378,9 +378,9 @@ void msg_cpm_result(uint8_t subf, uint8_t rc, int res) void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg) { - FRESULT res = 0; uint8_t drv; - char *np; + struct cpm_drive_s *p; + FRESULT res = 0; (void)subf; @@ -393,52 +393,22 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg) /* Get relative drive number */ drv = msg[1]; if ( drv >= CONFIG_CPM_MAX_DRIVE) { + debug_cpmsd("## failed: invalid relative drive number '%d'\n", drv); return msg_cpm_result(subf, 0x02, res); } + p = &drv_table[drv]; + p->flags &= ~DRV_FLG_OPEN; + p->dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2]; - drv_table[drv].dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2]; - - - if (drv_table[drv].img_name != NULL) { - debug_cpmsd("## %7lu close: '%s'\n", get_timer(0), drv_table[drv].img_name); - f_close(&drv_table[drv].fd); - drv_table[drv].dirty = false; - free(drv_table[drv].img_name); - drv_table[drv].img_name = NULL; - } - strcpy_P((char *)disk_buffer, PSTR("dsk0")); - disk_buffer[3] = msg[0] + '0'; - if (((np = getenv_char((char*)disk_buffer)) == NULL) || - ((drv_table[drv].img_name = strdup(np)) == NULL)) { + if (p->img_name == NULL) { + /* no file attached */ + debug_cpmsd("## failed: no file attached:\n"); return msg_cpm_result(subf, 0x03, res); } - - res = f_open(&drv_table[drv].fd, drv_table[drv].img_name, - FA_WRITE | FA_READ); - - debug_cpmsd("## %7lu open: '%s', (env: '%s'), res: %d\n", get_timer(0), - drv_table[drv].img_name, disk_buffer, res); - - if (!res && f_size(&drv_table[drv].fd) < CONFIG_CPM_DISKSIZE) { - unsigned int bw; - - debug_cpmsd(" expanding image file from %ld to %ld\n", - f_size(&drv_table[drv].fd), CONFIG_CPM_DISKSIZE); - - res = f_lseek(&drv_table[drv].fd, CONFIG_CPM_DISKSIZE-CONFIG_CPM_BLOCK_SIZE); - if (!res) { - memset(disk_buffer, 0xe5, CONFIG_CPM_BLOCK_SIZE); - res = f_write(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &bw); - if (res || bw < CONFIG_CPM_BLOCK_SIZE) { - debug_cpmsd(" failed! res: %d, bytes written: %u\n", res, bw); - } - drv_table[drv].dirty = true; - bg_setstat(handle_cpm_drv_to, 1); - } - } + p->flags |= DRV_FLG_OPEN; /* send result*/ msg_cpm_result(subf, 0x00, res); @@ -535,7 +505,7 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg) } if (dowrite && !res) { - drv_table[drv].dirty = true; + drv_table[drv].flags |= DRV_FLG_DIRTY; bg_setstat(handle_cpm_drv_to, 1); } diff --git a/include/z180-serv.h b/include/z180-serv.h index 5385ecd..b6a65b0 100644 --- a/include/z180-serv.h +++ b/include/z180-serv.h @@ -25,9 +25,13 @@ typedef uint8_t drv_opt_t; #define DRV_OPT_DEBUG (1<<1) /* Debug this drive */ #define DRV_OPT_REATTATCH (1<<7) /* Change existing attachment */ +typedef uint8_t drv_flag_t; +#define DRV_FLG_OPEN (1<<0) /* Drive is logged in from CP/M */ +#define DRV_FLG_DIRTY (2<<0) /* Unwritten data */ + struct cpm_drive_s { drv_opt_t opt; - bool dirty; + drv_flag_t flags; uint32_t dph; char *img_name; FIL fd; -- 2.39.2