]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
attach option: ro; do_msg_cpm_rw(): check status
authorLeo C <erbl259-lmu@yahoo.de>
Tue, 31 May 2016 10:51:18 +0000 (12:51 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 31 May 2016 10:51:18 +0000 (12:51 +0200)
avr/z180-serv.c

index ddb4622be4707bc3edb1a80c9bf5720c8bece717..d0d4cfcebc35d163d51f6b252c4d14d0f4014af6 100644 (file)
@@ -273,9 +273,14 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
                        return AT_NOT;
                }
 
-               p->opt = options & ~DRV_OPT_REATTATCH;
+               /* change options */
+               if ((p->opt ^ options) & DRV_OPT_RO) {
+                       f_close(&p->fd);
+                       res = f_open(&p->fd, p->img_name,
+                                       FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
+               }
 
-               /* TODO: change options */
+               p->opt = options & ~DRV_OPT_REATTATCH;
 
        } else {
 
@@ -292,11 +297,11 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
                        return AT_NOMEM;
 
                res = f_open(&p->fd, p->img_name,
-                               FA_WRITE | FA_READ);
+                               FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
 
                if (!res && f_size(&p->fd) < CONFIG_CPM_DISKSIZE) {
+#if 0
                        unsigned int bw;
-
                        debug_cpmsd("            expanding image file from %ld to %ld\n",
                                        f_size(&p->fd), CONFIG_CPM_DISKSIZE);
 
@@ -310,6 +315,11 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
                                p->flags |= DRV_FLG_DIRTY;
                                bg_setstat(handle_cpm_drv_to, 1);
                        }
+#else
+                       debug_cpmsd("            wrong image file zize: %ld, should be %ld\n",
+                                       f_size(&p->fd), CONFIG_CPM_DISKSIZE);
+                       res = 64;
+#endif
                }
                if (res) {
                        drv_detach(drv);
@@ -379,7 +389,7 @@ 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)
 {
        uint8_t drv;
-       struct cpm_drive_s *p;
+       struct cpm_drive_s *dp;
        FRESULT res = 0;
 
        (void)subf;
@@ -397,18 +407,18 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
                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];
+       dp = &drv_table[drv];
+       dp->flags &= ~DRV_FLG_OPEN;
+       dp->dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
 
 
-       if (p->img_name == NULL) {
+       if (dp->img_name == NULL) {
                /* no file attached */
                debug_cpmsd("##           failed: no file attached:\n");
                return msg_cpm_result(subf, 0x03, res);
        }
 
-       p->flags |= DRV_FLG_OPEN;
+       dp->flags |= DRV_FLG_OPEN;
 
        /* send  result*/
        msg_cpm_result(subf, 0x00, res);
@@ -436,10 +446,11 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
 void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
 {
        uint8_t drv;
+       struct cpm_drive_s *dp;
        uint32_t addr;
        uint32_t pos;
        uint8_t secs;
-       bool dowrite = (subf == 2);
+       bool dowrite;
        FRESULT res = 0;
        uint8_t rc = 0;
        bool buserr = 0;
@@ -453,6 +464,22 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
                return msg_cpm_result(subf, 0x02, res);
        }
 
+       dp = &drv_table[drv];
+
+       if (dp->img_name == NULL) {
+               /* no media */
+               return msg_cpm_result(subf, 0x06, res);
+       }
+
+       if (subf == 2) {
+               dowrite = true;
+               if (dp->opt & DRV_OPT_RO) {
+                       return msg_cpm_result(subf, 0x05, res);
+               }
+       } else {
+               dowrite = false;
+       }
+
        secs = msg[CNT];
        addr = ((uint32_t)msg[ADDR+2] << 16) + ((uint16_t)msg[ADDR+1] << 8) + msg[ADDR];
 
@@ -472,7 +499,7 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
                return msg_cpm_result(subf, 0x04, res);
        }
 
-       res = f_lseek(&drv_table[drv].fd, pos);
+       res = f_lseek(&dp->fd, pos);
 
        while (!res && secs--) {
                unsigned int brw;
@@ -484,9 +511,9 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
                                z80_read_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
                                z80_bus_cmd(Release);
                        }
-                       res = f_write(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
+                       res = f_write(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
                } else {
-                       res = f_read(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
+                       res = f_read(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
                        if (res == FR_OK) {
                                if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
                                        buserr = 1;
@@ -505,7 +532,7 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
        }
 
        if (dowrite && !res) {
-               drv_table[drv].flags |= DRV_FLG_DIRTY;
+               dp->flags |= DRV_FLG_DIRTY;
                bg_setstat(handle_cpm_drv_to, 1);
        }