1 /* #includes */ /*{{{C}}}*//*{{{*/
16 static const char *lookupFormat(DSK_GEOMETRY *geom, const char *name)
18 dsk_format_t fmt = FMT_180K;
21 while (dg_stdformat(NULL, fmt, &fname, NULL) == DSK_ERR_OK)
23 if (!strcmp(name, fname))
25 dg_stdformat(geom, fmt, &fname, NULL);
30 return "Unrecognised LibDsk geometry specification";
33 /* Device_open -- Open an image file */ /*{{{*/
34 const char *Device_open(struct Device *this, const char *filename, int mode, const char *deviceOpts)
41 /* Assume driver name & format name both fit in 80 characters, rather than
42 * malloccing the exact size */
43 if (deviceOpts == NULL)
45 e = dsk_open(&this->dev, filename, NULL, NULL);
50 strncpy(driverName, deviceOpts, 79);
52 format = strchr(driverName, ',');
58 e = dsk_open(&this->dev, filename, driverName, NULL);
61 if (e) return dsk_strerror(e);
65 boo = lookupFormat(&this->geom, format);
70 dsk_getgeom(this->dev, &this->geom);
75 /* Device_setGeometry -- Set disk geometry */ /*{{{*/
76 const char *Device_setGeometry(struct Device *this, int secLength, int sectrk, int tracks, off_t offset, const char *libdskGeometry)
80 this->secLength=secLength;
83 /* Must be an even multiple of sector size */
84 assert(offset%secLength==0);
86 /* If a geometry is named in diskdefs, use it */
87 if (libdskGeometry && libdskGeometry[0])
89 return lookupFormat(&this->geom, libdskGeometry);
92 this->geom.dg_secsize = secLength;
93 this->geom.dg_sectors = sectrk;
94 /* Did the autoprobe guess right about the number of sectors & cylinders? */
95 if (this->geom.dg_cylinders * this->geom.dg_heads == tracks) return NULL;
96 /* Otherwise we guess: <= 43 tracks: single-sided. Else double. This
97 * fails for 80-track single-sided if there are any such beasts */
100 this->geom.dg_cylinders = tracks;
101 this->geom.dg_heads = 1;
105 this->geom.dg_cylinders = tracks/2;
106 this->geom.dg_heads = 2;
111 /* Device_close -- Close an image file */ /*{{{*/
112 const char *Device_close(struct Device *this)
116 e = dsk_close(&this->dev);
117 return (e?dsk_strerror(e):(const char*)0);
120 /* Device_readSector -- read a physical sector */ /*{{{*/
121 const char *Device_readSector(const struct Device *this, int track, int sector, char *buf)
124 e = dsk_lread(this->dev, &this->geom, buf, (track * this->sectrk) + sector + this->offset/this->secLength);
125 return (e?dsk_strerror(e):(const char*)0);
128 /* Device_writeSector -- write physical sector */ /*{{{*/
129 const char *Device_writeSector(const struct Device *this, int track, int sector, const char *buf)
132 e = dsk_lwrite(this->dev, &this->geom, buf, (track * this->sectrk) + sector + this->offset/this->secLength);
133 return (e?dsk_strerror(e):(const char*)0);