+ if (!p->img_name) {
+ return EATNOT;
+ }
+
+ /* 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));
+ }
+
+ p->opt = options & ~DRV_OPT_REATTATCH;
+
+ } else {
+
+ if (p->img_name)
+ return EATALRDY;
+ if (drv_find_file_attached(filename) >= 0)
+ return EATOTHER;
+
+ p->opt = options;
+
+ /* new attachment */
+
+ if ((p->img_name = strdup(filename)) == NULL)
+ return ENOMEM;
+
+ res = f_open(&p->fd, p->img_name,
+ 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);
+
+ res = f_lseek(&p->fd, CONFIG_CPM_DISKSIZE-CONFIG_CPM_BLOCK_SIZE);
+ if (!res) {
+ memset(disk_buffer, 0xe5, CONFIG_CPM_BLOCK_SIZE);
+ res = f_write(&p->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);
+ }
+ p->flags |= DRV_FLG_DIRTY;
+ bg_setstat(handle_cpm_drv_to, 1);
+ }
+#else
+ drv_debug(SINGLE, PSTR("wrong image file size: %ld, should be %ld"),
+ f_size(&p->fd), CONFIG_CPM_DISKSIZE);
+ res = 64;
+#endif
+ }
+ if (res) {
+ drv_detach(drv);
+ return EATOPEN;
+ }
+ }
+
+ return ESUCCESS;
+}
+
+
+int cpm_drv_to(int state)
+{
+ static uint32_t ts;
+
+ switch(state) {
+ case 0:
+ break;
+
+ case 1:
+ ts = get_timer(0);
+ state = 2;
+ break;
+
+ case 2:
+ if (get_timer(ts) > 1000) {
+ for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) {
+ if (drv_table[i].flags & DRV_FLG_DIRTY) {
+ drv_table[i].flags &= ~DRV_FLG_DIRTY;
+ f_sync(&drv_table[i].fd);
+ drv = i;
+ drv_debug(SINGLE, PSTR("f_sync"));
+ }
+ }
+ state = 0;
+ }
+ }
+ return state;
+}
+
+static const FLASH char * const FLASH rc_messages[] = {
+ FSTR("OK"),
+ FSTR("Internal error: wrong message len"), /* 01 */
+ FSTR("Invalid relative drive #"), /* 02 */
+ FSTR("Bus timeout"), /* 03 */
+ FSTR("Access byond disk size"), /* 04 */
+ FSTR("Write protect"), /* 05 */
+ FSTR("No media"), /* 06 */
+ FSTR("R/W address == 0 !!!!"), /* 07 */
+ };
+
+void msg_cpm_result(uint8_t subf, uint8_t rc, int res)
+{
+ uint8_t result_msg[3];