src/mess/tools/imgtool/imghd.c
| r14482 | r14483 | |
| 48 | 48 | { |
| 49 | 49 | imgtoolerr_t err = IMGTOOLERR_SUCCESS; |
| 50 | 50 | UINT8 *cache = NULL; |
| 51 | | chd_file *chd = NULL; |
| 51 | chd_file chd; |
| 52 | 52 | chd_error rc; |
| 53 | 53 | UINT64 logicalbytes; |
| 54 | 54 | int hunknum, totalhunks; |
| 55 | | char metadata[256]; |
| 55 | astring metadata; |
| 56 | chd_codec_type compression[4] = { CHD_CODEC_NONE }; |
| 56 | 57 | |
| 57 | 58 | /* sanity check args */ |
| 58 | 59 | if (hunksize >= 2048) |
| r14482 | r14483 | |
| 74 | 75 | logicalbytes = (UINT64)cylinders * heads * sectors * seclen; |
| 75 | 76 | |
| 76 | 77 | /* create the new hard drive */ |
| 77 | | rc = chd_create_file(stream_core_file(stream), logicalbytes, hunksize, CHDCOMPRESSION_NONE, NULL); |
| 78 | rc = chd.create(*stream_core_file(stream), logicalbytes, hunksize, seclen, compression); |
| 78 | 79 | if (rc != CHDERR_NONE) |
| 79 | 80 | { |
| 80 | 81 | err = map_chd_error(rc); |
| r14482 | r14483 | |
| 82 | 83 | } |
| 83 | 84 | |
| 84 | 85 | /* open the new hard drive */ |
| 85 | | rc = chd_open_file(stream_core_file(stream), CHD_OPEN_READWRITE, NULL, &chd); |
| 86 | rc = chd.open(*stream_core_file(stream)); |
| 87 | |
| 86 | 88 | if (rc != CHDERR_NONE) |
| 87 | 89 | { |
| 88 | 90 | err = map_chd_error(rc); |
| r14482 | r14483 | |
| 90 | 92 | } |
| 91 | 93 | |
| 92 | 94 | /* write the metadata */ |
| 93 | | sprintf(metadata, HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, seclen); |
| 94 | | err = (imgtoolerr_t)chd_set_metadata(chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1, 0); |
| 95 | metadata.format(HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, seclen); |
| 96 | err = (imgtoolerr_t)chd.write_metadata(HARD_DISK_METADATA_TAG, 0, metadata); |
| 95 | 97 | if (rc != CHDERR_NONE) |
| 96 | 98 | { |
| 97 | 99 | err = map_chd_error(rc); |
| r14482 | r14483 | |
| 111 | 113 | totalhunks = (logicalbytes + hunksize - 1) / hunksize; |
| 112 | 114 | for (hunknum = 0; hunknum < totalhunks; hunknum++) |
| 113 | 115 | { |
| 114 | | rc = chd_write(chd, hunknum, cache); |
| 116 | rc = chd.write_units(hunknum, cache); |
| 115 | 117 | if (rc) |
| 116 | 118 | { |
| 117 | 119 | err = IMGTOOLERR_WRITEERROR; |
| r14482 | r14483 | |
| 123 | 125 | done: |
| 124 | 126 | if (cache) |
| 125 | 127 | free(cache); |
| 126 | | if (chd) |
| 127 | | chd_close(chd); |
| 128 | 128 | return err; |
| 129 | 129 | } |
| 130 | 130 | |
| r14482 | r14483 | |
| 143 | 143 | hard_disk->hard_disk = NULL; |
| 144 | 144 | hard_disk->chd = NULL; |
| 145 | 145 | |
| 146 | | chderr = chd_open_file(stream_core_file(stream), stream_isreadonly(stream) ? CHD_OPEN_READ : CHD_OPEN_READWRITE, NULL, &hard_disk->chd); |
| 146 | chderr = hard_disk->chd->open(*stream_core_file(stream), stream_isreadonly(stream)); |
| 147 | 147 | if (chderr) |
| 148 | 148 | { |
| 149 | 149 | err = map_chd_error(chderr); |
| r14482 | r14483 | |
| 178 | 178 | hard_disk_close(disk->hard_disk); |
| 179 | 179 | disk->hard_disk = NULL; |
| 180 | 180 | } |
| 181 | | if (disk->chd) |
| 182 | | { |
| 183 | | chd_close(disk->chd); |
| 184 | | disk->chd = NULL; |
| 185 | | } |
| 186 | 181 | if (disk->stream) |
| 187 | 182 | stream_close(disk->stream); |
| 188 | 183 | } |