src/mess/tools/imgtool/modules/cybikoxt.c
| r0 | r8652 | |
| 1 | /*
|
| 2 | |
| 3 | Cybiko Xtreme File System
|
| 4 | |
| 5 | (c) 2010 Tim Schuerewegen
|
| 6 | |
| 7 | */
|
| 8 | |
| 9 | #include "imgtool.h"
|
| 10 | |
| 11 | #include <zlib.h>
|
| 12 | |
| 13 | typedef struct _cybiko_file_system cybiko_file_system;
|
| 14 | struct _cybiko_file_system
|
| 15 | {
|
| 16 | imgtool_stream *stream;
|
| 17 | UINT32 page_count, page_size, block_count_boot, block_count_file;
|
| 18 | UINT16 write_count;
|
| 19 | };
|
| 20 | |
| 21 | typedef struct _cybiko_iter cybiko_iter;
|
| 22 | struct _cybiko_iter
|
| 23 | {
|
| 24 | UINT16 block;
|
| 25 | };
|
| 26 | |
| 27 | typedef struct _cfs_file cfs_file;
|
| 28 | struct _cfs_file
|
| 29 | {
|
| 30 | char name[64]; // name of the file
|
| 31 | UINT32 date; // date/time of the file (seconds since 1900/01/01)
|
| 32 | UINT32 size; // size of the file
|
| 33 | UINT32 blocks; // number of blocks occupied by the file
|
| 34 | };
|
| 35 | |
| 36 | enum
|
| 37 | {
|
| 38 | BLOCK_TYPE_INVALID,
|
| 39 | BLOCK_TYPE_BOOT,
|
| 40 | BLOCK_TYPE_FILE
|
| 41 | };
|
| 42 | |
| 43 | #define MAX_PAGE_SIZE (264 * 2)
|
| 44 | |
| 45 | #define INVALID_FILE_ID 0xFFFF
|
| 46 | |
| 47 | #define BLOCK_USED(x) (x[0] & 0x80)
|
| 48 | #define BLOCK_FILE_ID(x) buffer_read_16_be( x + 2)
|
| 49 | #define BLOCK_PART_ID(x) buffer_read_16_be( x + 4)
|
| 50 | #define BLOCK_FILENAME(x) (char*)(x + 7)
|
| 51 | |
| 52 | #define FILE_HEADER_SIZE 0x48
|
| 53 | |
| 54 | // 2208988800 is the number of seconds between 1900/01/01 and 1970/01/01
|
| 55 | |
| 56 | static time_t time_crack( UINT32 cfs_time)
|
| 57 | {
|
| 58 | return (time_t)(cfs_time - 2208988800UL);
|
| 59 | }
|
| 60 | |
| 61 | static UINT32 time_setup( time_t ansi_time)
|
| 62 | {
|
| 63 | return (UINT32)(ansi_time + 2208988800UL);
|
| 64 | }
|
| 65 | |
| 66 | static UINT32 buffer_read_32_be( UINT8 *buffer)
|
| 67 | {
|
| 68 | return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3] << 0);
|
| 69 | }
|
| 70 | |
| 71 | static UINT16 buffer_read_16_be( UINT8 *buffer)
|
| 72 | {
|
| 73 | return (buffer[0] << 8) | (buffer[1] << 0);
|
| 74 | }
|
| 75 | |
| 76 | static void buffer_write_32_be( UINT8 *buffer, UINT32 data)
|
| 77 | {
|
| 78 | buffer[0] = (data >> 24) & 0xFF;
|
| 79 | buffer[1] = (data >> 16) & 0xFF;
|
| 80 | buffer[2] = (data >> 8) & 0xFF;
|
| 81 | buffer[3] = (data >> 0) & 0xFF;
|
| 82 | }
|
| 83 | |
| 84 | static void buffer_write_16_be( UINT8 *buffer, UINT16 data)
|
| 85 | {
|
| 86 | buffer[0] = (data >> 8) & 0xFF;
|
| 87 | buffer[1] = (data >> 0) & 0xFF;
|
| 88 | }
|
| 89 | |
| 90 | // page = crc (2) + data (x)
|
| 91 | |
| 92 | static UINT16 page_buffer_calc_checksum( UINT8 *data, UINT32 size)
|
| 93 | {
|
| 94 | int i;
|
| 95 | UINT32 val = 0;
|
| 96 | for (i = 0; i < size; i++)
|
| 97 | {
|
| 98 | val = (val ^ data[i] ^ i) << 1;
|
| 99 | val = val | ((val >> 16) & 0x0001);
|
| 100 | }
|
| 101 | return val;
|
| 102 | }
|
| 103 | |
| 104 | static int page_buffer_verify( UINT8 *buffer, UINT32 size, int block_type)
|
| 105 | {
|
| 106 | // checksum
|
| 107 | if (block_type == BLOCK_TYPE_FILE)
|
| 108 | {
|
| 109 | UINT32 checksum_page, checksum_calc;
|
| 110 | checksum_calc = page_buffer_calc_checksum( buffer + 2, size - 2);
|
| 111 | checksum_page = buffer_read_16_be( buffer + 0);
|
| 112 | if (checksum_calc != checksum_page) return FALSE;
|
| 113 | }
|
| 114 | // ok
|
| 115 | return TRUE;
|
| 116 | }
|
| 117 | |
| 118 | static int cfs_block_to_page( cybiko_file_system *cfs, int block_type, UINT32 block, UINT32 *page)
|
| 119 | {
|
| 120 | switch (block_type)
|
| 121 | {
|
| 122 | case BLOCK_TYPE_BOOT : if (page) *page = block; return TRUE;
|
| 123 | case BLOCK_TYPE_FILE : if (page) *page = block + cfs->block_count_boot; return TRUE;
|
| 124 | default : return FALSE;
|
| 125 | }
|
| 126 | }
|
| 127 | |
| 128 | static int cfs_page_to_block( cybiko_file_system *cfs, UINT32 page, int *block_type, UINT32 *block)
|
| 129 | {
|
| 130 | UINT32 tmp = page;
|
| 131 | // boot block
|
| 132 | if (tmp < cfs->block_count_boot)
|
| 133 | {
|
| 134 | if (block_type) *block_type = BLOCK_TYPE_BOOT;
|
| 135 | if (block) *block = tmp;
|
| 136 | return TRUE;
|
| 137 | }
|
| 138 | tmp -= cfs->block_count_boot;
|
| 139 | // file block
|
| 140 | if (tmp < cfs->block_count_file)
|
| 141 | {
|
| 142 | if (block_type) *block_type = BLOCK_TYPE_FILE;
|
| 143 | if (block) *block = tmp;
|
| 144 | return TRUE;
|
| 145 | }
|
| 146 | tmp -= cfs->block_count_file;
|
| 147 | // error
|
| 148 | return FALSE;
|
| 149 | }
|
| 150 | |
| 151 | static int cfs_page_read( cybiko_file_system *cfs, UINT8 *buffer, UINT32 page)
|
| 152 | {
|
| 153 | if (page >= cfs->page_count) return FALSE;
|
| 154 | stream_seek( cfs->stream, page * cfs->page_size, SEEK_SET);
|
| 155 | stream_read( cfs->stream, buffer, cfs->page_size);
|
| 156 | return TRUE;
|
| 157 | }
|
| 158 | |
| 159 | static int cfs_page_write( cybiko_file_system *cfs, UINT8 *buffer, UINT32 page)
|
| 160 | {
|
| 161 | if (page >= cfs->page_count) return FALSE;
|
| 162 | stream_seek( cfs->stream, page * cfs->page_size, SEEK_SET);
|
| 163 | stream_write( cfs->stream, buffer, cfs->page_size);
|
| 164 | return TRUE;
|
| 165 | }
|
| 166 | |
| 167 | static int cfs_block_read( cybiko_file_system *cfs, UINT8 *buffer, int block_type, UINT32 block)
|
| 168 | {
|
| 169 | UINT8 buffer_page[MAX_PAGE_SIZE];
|
| 170 | UINT32 page;
|
| 171 | if (!cfs_block_to_page( cfs, block_type, block, &page)) return FALSE;
|
| 172 | if (!cfs_page_read( cfs, buffer_page, page)) return FALSE;
|
| 173 | memcpy( buffer, buffer_page + 2, cfs->page_size - 2);
|
| 174 | return TRUE;
|
| 175 | }
|
| 176 | |
| 177 | static int cfs_block_write( cybiko_file_system *cfs, UINT8 *buffer, int block_type, UINT32 block)
|
| 178 | {
|
| 179 | UINT8 buffer_page[MAX_PAGE_SIZE];
|
| 180 | UINT32 page;
|
| 181 | UINT16 checksum;
|
| 182 | memcpy( buffer_page + 2, buffer, cfs->page_size - 2);
|
| 183 | if (block_type == BLOCK_TYPE_BOOT)
|
| 184 | {
|
| 185 | checksum = 0xFFFF;
|
| 186 | }
|
| 187 | else
|
| 188 | {
|
| 189 | checksum = page_buffer_calc_checksum( buffer_page + 2, cfs->page_size - 2);
|
| 190 | }
|
| 191 | buffer_write_16_be( buffer_page + 0, checksum);
|
| 192 | if (!cfs_block_to_page( cfs, block_type, block, &page)) return FALSE;
|
| 193 | if (!cfs_page_write( cfs, buffer_page, page)) return FALSE;
|
| 194 | return TRUE;
|
| 195 | }
|
| 196 | |
| 197 | static int cfs_file_delete( cybiko_file_system *cfs, UINT16 file_id)
|
| 198 | {
|
| 199 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 200 | int i;
|
| 201 | for (i=0;i<cfs->block_count_file;i++)
|
| 202 | {
|
| 203 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return FALSE;
|
| 204 | if (BLOCK_USED(buffer) && (BLOCK_FILE_ID(buffer) == file_id))
|
| 205 | {
|
| 206 | buffer[0] &= ~0x80;
|
| 207 | if (!cfs_block_write( cfs, buffer, BLOCK_TYPE_FILE, i)) return FALSE;
|
| 208 | }
|
| 209 | }
|
| 210 | return TRUE;
|
| 211 | }
|
| 212 | |
| 213 | static int cfs_file_info( cybiko_file_system *cfs, UINT16 file_id, cfs_file *file)
|
| 214 | {
|
| 215 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 216 | int i;
|
| 217 | file->blocks = file->size = 0;
|
| 218 | for (i=0;i<cfs->block_count_file;i++)
|
| 219 | {
|
| 220 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return FALSE;
|
| 221 | if (BLOCK_USED(buffer) && (BLOCK_FILE_ID(buffer) == file_id))
|
| 222 | {
|
| 223 | if (BLOCK_PART_ID(buffer) == 0)
|
| 224 | {
|
| 225 | strcpy( file->name, BLOCK_FILENAME(buffer));
|
| 226 | file->date = buffer_read_32_be( buffer + 6 + FILE_HEADER_SIZE - 4);
|
| 227 | }
|
| 228 | file->size += buffer[1];
|
| 229 | file->blocks++;
|
| 230 | }
|
| 231 | }
|
| 232 | return (file->blocks > 0) ? TRUE : FALSE;
|
| 233 | }
|
| 234 | |
| 235 | static int cfs_file_find( cybiko_file_system *cfs, const char *filename, UINT16 *file_id)
|
| 236 | {
|
| 237 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 238 | int i;
|
| 239 | for (i=0;i<cfs->block_count_file;i++)
|
| 240 | {
|
| 241 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return FALSE;
|
| 242 | if (BLOCK_USED(buffer) && (BLOCK_PART_ID(buffer) == 0) && (strcmp( filename, BLOCK_FILENAME(buffer)) == 0))
|
| 243 | {
|
| 244 | *file_id = i;
|
| 245 | return TRUE;
|
| 246 | }
|
| 247 | }
|
| 248 | return FALSE;
|
| 249 | }
|
| 250 | |
| 251 | static int cfs_verify( cybiko_file_system *cfs)
|
| 252 | {
|
| 253 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 254 | int i, block_type;
|
| 255 | for (i=0;i<cfs->page_count;i++)
|
| 256 | {
|
| 257 | if (!cfs_page_read( cfs, buffer, i)) return FALSE;
|
| 258 | if (!cfs_page_to_block( cfs, i, &block_type, NULL)) return FALSE;
|
| 259 | if (!page_buffer_verify( buffer, cfs->page_size, block_type)) return FALSE;
|
| 260 | }
|
| 261 | return TRUE;
|
| 262 | }
|
| 263 | |
| 264 | static int cfs_init( cybiko_file_system *cfs, imgtool_stream *stream)
|
| 265 | {
|
| 266 | cfs->stream = stream;
|
| 267 | cfs->page_count = 2005;
|
| 268 | cfs->page_size = 258;
|
| 269 | cfs->block_count_boot = 5;
|
| 270 | cfs->block_count_file = cfs->page_count - cfs->block_count_boot;
|
| 271 | cfs->write_count = 0;
|
| 272 | return TRUE;
|
| 273 | }
|
| 274 | |
| 275 | static int cfs_format( cybiko_file_system *cfs)
|
| 276 | {
|
| 277 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 278 | int i;
|
| 279 | // boot blocks
|
| 280 | memset( buffer, 0xFF, sizeof( buffer));
|
| 281 | for (i=0;i<cfs->block_count_boot;i++)
|
| 282 | {
|
| 283 | if (!cfs_block_write( cfs, buffer, BLOCK_TYPE_BOOT, i)) return FALSE;
|
| 284 | }
|
| 285 | // file blocks
|
| 286 | memset( buffer, 0xFF, sizeof( buffer));
|
| 287 | buffer[0] &= ~0x80;
|
| 288 | for (i=0;i<cfs->block_count_file;i++)
|
| 289 | {
|
| 290 | if (!cfs_block_write( cfs, buffer, BLOCK_TYPE_FILE, i)) return FALSE;
|
| 291 | }
|
| 292 | // padding
|
| 293 | buffer[0] = 0xFF;
|
| 294 | for (i=0;i<0x1B56;i++)
|
| 295 | {
|
| 296 | stream_write( cfs->stream, buffer, 1);
|
| 297 | }
|
| 298 | // ok
|
| 299 | return TRUE;
|
| 300 | }
|
| 301 | |
| 302 | static UINT16 cfs_calc_free_blocks( cybiko_file_system *cfs)
|
| 303 | {
|
| 304 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 305 | int i;
|
| 306 | UINT16 blocks = 0;
|
| 307 | for (i=0;i<cfs->block_count_file;i++)
|
| 308 | {
|
| 309 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return 0;
|
| 310 | if (!BLOCK_USED(buffer)) blocks++;
|
| 311 | }
|
| 312 | return blocks;
|
| 313 | }
|
| 314 | |
| 315 | static UINT32 cfs_calc_free_space( cybiko_file_system *cfs, UINT16 blocks)
|
| 316 | {
|
| 317 | UINT32 free_space;
|
| 318 | free_space = blocks * ((cfs->page_size - 2) - 6);
|
| 319 | if (free_space > 0) free_space -= FILE_HEADER_SIZE;
|
| 320 | return free_space;
|
| 321 | }
|
| 322 | |
| 323 | static imgtoolerr_t cybiko_image_open( imgtool_image *image, imgtool_stream *stream)
|
| 324 | {
|
| 325 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 326 | // init
|
| 327 | if (!cfs_init( cfs, stream)) return IMGTOOLERR_CORRUPTIMAGE;
|
| 328 | // verify
|
| 329 | if (!cfs_verify( cfs)) return IMGTOOLERR_CORRUPTIMAGE;
|
| 330 | // ok
|
| 331 | return IMGTOOLERR_SUCCESS;
|
| 332 | }
|
| 333 | |
| 334 | static void cybiko_image_close( imgtool_image *image)
|
| 335 | {
|
| 336 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 337 | stream_close( cfs->stream);
|
| 338 | }
|
| 339 | |
| 340 | static imgtoolerr_t cybiko_image_create( imgtool_image *image, imgtool_stream *stream, option_resolution *opts)
|
| 341 | {
|
| 342 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 343 | // init
|
| 344 | if (!cfs_init( cfs, stream)) return IMGTOOLERR_CORRUPTIMAGE;
|
| 345 | // format
|
| 346 | if (!cfs_format( cfs)) return IMGTOOLERR_CORRUPTIMAGE;
|
| 347 | // ok
|
| 348 | return IMGTOOLERR_SUCCESS;
|
| 349 | }
|
| 350 | |
| 351 | static imgtoolerr_t cybiko_image_begin_enum( imgtool_directory *enumeration, const char *path)
|
| 352 | {
|
| 353 | cybiko_iter *iter = (cybiko_iter*)imgtool_directory_extrabytes( enumeration);
|
| 354 | iter->block = 0;
|
| 355 | return IMGTOOLERR_SUCCESS;
|
| 356 | }
|
| 357 | |
| 358 | static imgtoolerr_t cybiko_image_next_enum( imgtool_directory *enumeration, imgtool_dirent *ent)
|
| 359 | {
|
| 360 | imgtool_image *image = imgtool_directory_image( enumeration);
|
| 361 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 362 | cybiko_iter *iter = (cybiko_iter*)imgtool_directory_extrabytes( enumeration);
|
| 363 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 364 | UINT16 file_id = INVALID_FILE_ID;
|
| 365 | cfs_file file;
|
| 366 | // find next file
|
| 367 | while (iter->block < cfs->block_count_file)
|
| 368 | {
|
| 369 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, iter->block++)) return IMGTOOLERR_READERROR;
|
| 370 | if (BLOCK_USED(buffer) && (BLOCK_PART_ID(buffer) == 0))
|
| 371 | {
|
| 372 | file_id = BLOCK_FILE_ID(buffer);
|
| 373 | break;
|
| 374 | }
|
| 375 | }
|
| 376 | // get file information
|
| 377 | if ((file_id != INVALID_FILE_ID) && cfs_file_info( cfs, file_id, &file))
|
| 378 | {
|
| 379 | strcpy( ent->filename, file.name);
|
| 380 | ent->filesize = file.size;
|
| 381 | ent->lastmodified_time = time_crack( file.date);
|
| 382 | ent->filesize = file.size;
|
| 383 | }
|
| 384 | else
|
| 385 | {
|
| 386 | ent->eof = 1;
|
| 387 | }
|
| 388 | // ok
|
| 389 | return IMGTOOLERR_SUCCESS;
|
| 390 | }
|
| 391 | |
| 392 | static void cybiko_image_close_enum( imgtool_directory *enumeration)
|
| 393 | {
|
| 394 | // nothing
|
| 395 | }
|
| 396 | |
| 397 | static imgtoolerr_t cybiko_image_free_space( imgtool_partition *partition, UINT64 *size)
|
| 398 | {
|
| 399 | imgtool_image *image = imgtool_partition_image( partition);
|
| 400 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 401 | if (size) *size = cfs_calc_free_space( cfs, cfs_calc_free_blocks( cfs));
|
| 402 | return IMGTOOLERR_SUCCESS;
|
| 403 | }
|
| 404 | |
| 405 | static imgtoolerr_t cybiko_image_read_file( imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *destf)
|
| 406 | {
|
| 407 | imgtool_image *image = imgtool_partition_image( partition);
|
| 408 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 409 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 410 | UINT16 file_id, part_id = 0, old_part_id;
|
| 411 | int i;
|
| 412 | // check filename
|
| 413 | if (strlen( filename) > 58) return IMGTOOLERR_BADFILENAME;
|
| 414 | // find file
|
| 415 | if (!cfs_file_find( cfs, filename, &file_id)) return IMGTOOLERR_FILENOTFOUND;
|
| 416 | // read file
|
| 417 | do
|
| 418 | {
|
| 419 | old_part_id = part_id;
|
| 420 | for (i=0;i<cfs->block_count_file;i++)
|
| 421 | {
|
| 422 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return IMGTOOLERR_READERROR;
|
| 423 | if (BLOCK_USED(buffer) && (BLOCK_FILE_ID(buffer) == file_id) && (BLOCK_PART_ID(buffer) == part_id))
|
| 424 | {
|
| 425 | stream_write( destf, buffer + 6 + ((part_id == 0) ? FILE_HEADER_SIZE : 0), buffer[1]);
|
| 426 | part_id++;
|
| 427 | }
|
| 428 | }
|
| 429 | } while (old_part_id != part_id);
|
| 430 | // ok
|
| 431 | return IMGTOOLERR_SUCCESS;
|
| 432 | }
|
| 433 | |
| 434 | static imgtoolerr_t cybiko_image_write_file( imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *sourcef, option_resolution *opts)
|
| 435 | {
|
| 436 | imgtool_image *image = imgtool_partition_image( partition);
|
| 437 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 438 | UINT8 buffer[MAX_PAGE_SIZE];
|
| 439 | UINT16 file_id, part_id = 0, free_blocks;
|
| 440 | UINT64 bytes_left;
|
| 441 | cfs_file file;
|
| 442 | int i;
|
| 443 | // check filename
|
| 444 | if (strlen( filename) > 58) return IMGTOOLERR_BADFILENAME;
|
| 445 | // find file
|
| 446 | if (!cfs_file_find( cfs, filename, &file_id)) file_id = INVALID_FILE_ID;
|
| 447 | // check free space
|
| 448 | free_blocks = cfs_calc_free_blocks( cfs);
|
| 449 | if (file_id != INVALID_FILE_ID)
|
| 450 | {
|
| 451 | if (!cfs_file_info( cfs, file_id, &file)) return IMGTOOLERR_UNEXPECTED;
|
| 452 | free_blocks += file.blocks;
|
| 453 | }
|
| 454 | if (cfs_calc_free_space( cfs, free_blocks) < stream_size( sourcef)) return IMGTOOLERR_NOSPACE;
|
| 455 | // delete file
|
| 456 | if (file_id != INVALID_FILE_ID)
|
| 457 | {
|
| 458 | if (!cfs_file_delete( cfs, file_id)) return IMGTOOLERR_UNEXPECTED;
|
| 459 | }
|
| 460 | // create/write destination file
|
| 461 | bytes_left = stream_size( sourcef);
|
| 462 | i = 0;
|
| 463 | while (i < cfs->block_count_file)
|
| 464 | {
|
| 465 | if (!cfs_block_read( cfs, buffer, BLOCK_TYPE_FILE, i)) return IMGTOOLERR_READERROR;
|
| 466 | if (!BLOCK_USED(buffer))
|
| 467 | {
|
| 468 | if (part_id == 0) file_id = i;
|
| 469 | memset( buffer, 0xFF, cfs->page_size - 0x02);
|
| 470 | buffer[0] = 0x80;
|
| 471 | buffer[1] = (cfs->page_size - 2) - 6 - ((part_id == 0) ? FILE_HEADER_SIZE : 0);
|
| 472 | if (bytes_left < buffer[1]) buffer[1] = bytes_left;
|
| 473 | buffer_write_16_be( buffer + 2, file_id);
|
| 474 | buffer_write_16_be( buffer + 4, part_id);
|
| 475 | if (part_id == 0)
|
| 476 | {
|
| 477 | buffer[6] = 0x20;
|
| 478 | strcpy( BLOCK_FILENAME(buffer), filename);
|
| 479 | buffer_write_32_be( buffer + 6 + FILE_HEADER_SIZE - 4, time_setup( time( NULL)));
|
| 480 | stream_read( sourcef, buffer + 6 + FILE_HEADER_SIZE, buffer[1]);
|
| 481 | }
|
| 482 | else
|
| 483 | {
|
| 484 | stream_read( sourcef, buffer + 6, buffer[1]);
|
| 485 | }
|
| 486 | if (!cfs_block_write( cfs, buffer, BLOCK_TYPE_FILE, i)) return IMGTOOLERR_WRITEERROR;
|
| 487 | bytes_left -= buffer[1];
|
| 488 | if (bytes_left == 0) break;
|
| 489 | part_id++;
|
| 490 | }
|
| 491 | i++;
|
| 492 | }
|
| 493 | // ok
|
| 494 | return IMGTOOLERR_SUCCESS;
|
| 495 | }
|
| 496 | |
| 497 | static imgtoolerr_t cybiko_image_delete_file( imgtool_partition *partition, const char *filename)
|
| 498 | {
|
| 499 | imgtool_image *image = imgtool_partition_image( partition);
|
| 500 | cybiko_file_system *cfs = (cybiko_file_system*)imgtool_image_extra_bytes( image);
|
| 501 | UINT16 file_id;
|
| 502 | // check filename
|
| 503 | if (strlen( filename) > 58) return IMGTOOLERR_BADFILENAME;
|
| 504 | // find file
|
| 505 | if (!cfs_file_find( cfs, filename, &file_id)) return IMGTOOLERR_FILENOTFOUND;
|
| 506 | // delete file
|
| 507 | if (!cfs_file_delete( cfs, file_id)) return IMGTOOLERR_UNEXPECTED;
|
| 508 | // ok
|
| 509 | return IMGTOOLERR_SUCCESS;
|
| 510 | }
|
| 511 | |
| 512 | void cybikoxt_get_info( const imgtool_class *imgclass, UINT32 state, union imgtoolinfo *info)
|
| 513 | {
|
| 514 | switch (state)
|
| 515 | {
|
| 516 | // --- the following bits of info are returned as 64-bit signed integers ---
|
| 517 | case IMGTOOLINFO_INT_IMAGE_EXTRA_BYTES : info->i = sizeof( cybiko_file_system); break;
|
| 518 | case IMGTOOLINFO_INT_DIRECTORY_EXTRA_BYTES : info->i = sizeof( cybiko_iter); break;
|
| 519 | // case IMGTOOLINFO_INT_SUPPORTS_CREATION_TIME : info->i = 1; break;
|
| 520 | case IMGTOOLINFO_INT_SUPPORTS_LASTMODIFIED_TIME : info->i = 1; break;
|
| 521 | // case IMGTOOLINFO_INT_BLOCK_SIZE : info->i = 264; break;
|
| 522 | // --- the following bits of info are returned as pointers to data or functions ---
|
| 523 | case IMGTOOLINFO_PTR_OPEN : info->open = cybiko_image_open; break;
|
| 524 | case IMGTOOLINFO_PTR_CREATE : info->create = cybiko_image_create; break;
|
| 525 | case IMGTOOLINFO_PTR_CLOSE : info->close = cybiko_image_close; break;
|
| 526 | case IMGTOOLINFO_PTR_BEGIN_ENUM : info->begin_enum = cybiko_image_begin_enum; break;
|
| 527 | case IMGTOOLINFO_PTR_NEXT_ENUM : info->next_enum = cybiko_image_next_enum; break;
|
| 528 | case IMGTOOLINFO_PTR_CLOSE_ENUM : info->close_enum = cybiko_image_close_enum; break;
|
| 529 | case IMGTOOLINFO_PTR_FREE_SPACE : info->free_space = cybiko_image_free_space; break;
|
| 530 | case IMGTOOLINFO_PTR_READ_FILE : info->read_file = cybiko_image_read_file; break;
|
| 531 | case IMGTOOLINFO_PTR_WRITE_FILE : info->write_file = cybiko_image_write_file; break;
|
| 532 | case IMGTOOLINFO_PTR_DELETE_FILE : info->delete_file = cybiko_image_delete_file; break;
|
| 533 | // --- the following bits of info are returned as NULL-terminated strings ---
|
| 534 | case IMGTOOLINFO_STR_NAME : strcpy( info->s = imgtool_temp_str(), "cybikoxt"); break;
|
| 535 | case IMGTOOLINFO_STR_DESCRIPTION : strcpy( info->s = imgtool_temp_str(), "Cybiko Xtreme File System"); break;
|
| 536 | case IMGTOOLINFO_STR_FILE : strcpy( info->s = imgtool_temp_str(), __FILE__); break;
|
| 537 | case IMGTOOLINFO_STR_FILE_EXTENSIONS : strcpy( info->s = imgtool_temp_str(), "bin,nv"); break;
|
| 538 | case IMGTOOLINFO_STR_EOLN : strcpy( info->s = imgtool_temp_str(), "\r\n"); break;
|
| 539 | }
|
| 540 | }
|