1 /* #includes */ /*{{{C}}}*//*{{{*/
12 static const char *lookupFormat(DSK_GEOMETRY *geom, const char *name)
14 dsk_format_t fmt = FMT_180K;
17 while (dg_stdformat(NULL, fmt, &fname, NULL) == DSK_ERR_OK)
19 if (!strcmp(name, fname))
21 dg_stdformat(geom, fmt, &fname, NULL);
26 return "Unrecognised LibDsk geometry specification";
29 /* Device_open -- Open an image file */ /*{{{*/
30 const char *Device_open(struct Device *this, const char *filename, int mode, const char *deviceOpts)
37 /* Assume driver name & format name both fit in 80 characters, rather than
38 * malloccing the exact size */
39 if (deviceOpts == NULL)
41 e = dsk_open(&this->dev, filename, NULL, NULL);
46 strncpy(driverName, deviceOpts, 79);
48 format = strchr(driverName, ',');
54 e = dsk_open(&this->dev, filename, driverName, NULL);
57 if (e) return dsk_strerror(e);
61 boo = lookupFormat(&this->geom, format);
66 dsk_getgeom(this->dev, &this->geom);
71 /* Device_setGeometry -- Set disk geometry */ /*{{{*/
72 const char *Device_setGeometry(struct Device *this, int secLength, int sectrk, int tracks, off_t offset, const char *libdskGeometry)
76 this->secLength=secLength;
79 /* Must be an even multiple of sector size */
80 assert(offset%secLength==0);
82 /* If a geometry is named in diskdefs, use it */
83 if (libdskGeometry && libdskGeometry[0])
85 return lookupFormat(&this->geom, libdskGeometry);
88 this->geom.dg_secsize = secLength;
89 this->geom.dg_sectors = sectrk;
90 /* Did the autoprobe guess right about the number of sectors & cylinders? */
91 if (this->geom.dg_cylinders * this->geom.dg_heads == tracks) return NULL;
92 /* Otherwise we guess: <= 43 tracks: single-sided. Else double. This
93 * fails for 80-track single-sided if there are any such beasts */
96 this->geom.dg_cylinders = tracks;
97 this->geom.dg_heads = 1;
101 this->geom.dg_cylinders = tracks/2;
102 this->geom.dg_heads = 2;
107 /* Device_close -- Close an image file */ /*{{{*/
108 const char *Device_close(struct Device *this)
112 e = dsk_close(&this->dev);
113 return (e?dsk_strerror(e):(const char*)0);
116 /* Device_readSector -- read a physical sector */ /*{{{*/
117 const char *Device_readSector(const struct Device *this, int track, int sector, char *buf)
120 e = dsk_lread(this->dev, &this->geom, buf, (track * this->sectrk) + sector + this->offset/this->secLength);
121 return (e?dsk_strerror(e):(const char*)0);
124 /* Device_writeSector -- write physical sector */ /*{{{*/
125 const char *Device_writeSector(const struct Device *this, int track, int sector, const char *buf)
128 e = dsk_lwrite(this->dev, &this->geom, buf, (track * this->sectrk) + sector + this->offset/this->secLength);
129 return (e?dsk_strerror(e):(const char*)0);