bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Block driver for DMG images |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004 Johannes E. Schindelin |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 25 | #include "block_int.h" |
| 26 | #include "bswap.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 27 | #include "module.h" |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 28 | #include <zlib.h> |
| 29 | |
| 30 | typedef struct BDRVDMGState { |
| 31 | int fd; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 32 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 33 | /* each chunk contains a certain number of sectors, |
| 34 | * offsets[i] is the offset in the .dmg file, |
| 35 | * lengths[i] is the length of the compressed chunk, |
| 36 | * sectors[i] is the sector beginning at offsets[i], |
| 37 | * sectorcounts[i] is the number of sectors in that chunk, |
| 38 | * the sectors array is ordered |
| 39 | * 0<=i<n_chunks */ |
| 40 | |
| 41 | uint32_t n_chunks; |
| 42 | uint32_t* types; |
| 43 | uint64_t* offsets; |
| 44 | uint64_t* lengths; |
| 45 | uint64_t* sectors; |
| 46 | uint64_t* sectorcounts; |
| 47 | uint32_t current_chunk; |
bellard | 28a5c9c | 2006-02-01 21:43:52 +0000 | [diff] [blame] | 48 | uint8_t *compressed_chunk; |
| 49 | uint8_t *uncompressed_chunk; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 50 | z_stream zstream; |
| 51 | } BDRVDMGState; |
| 52 | |
| 53 | static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename) |
| 54 | { |
| 55 | int len=strlen(filename); |
| 56 | if(len>4 && !strcmp(filename+len-4,".dmg")) |
| 57 | return 2; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static off_t read_off(int fd) |
| 62 | { |
| 63 | uint64_t buffer; |
| 64 | if(read(fd,&buffer,8)<8) |
| 65 | return 0; |
| 66 | return be64_to_cpu(buffer); |
| 67 | } |
| 68 | |
| 69 | static off_t read_uint32(int fd) |
| 70 | { |
| 71 | uint32_t buffer; |
| 72 | if(read(fd,&buffer,4)<4) |
| 73 | return 0; |
| 74 | return be32_to_cpu(buffer); |
| 75 | } |
| 76 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 77 | static int dmg_open(BlockDriverState *bs, const char *filename, int flags) |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 78 | { |
| 79 | BDRVDMGState *s = bs->opaque; |
| 80 | off_t info_begin,info_end,last_in_offset,last_out_offset; |
| 81 | uint32_t count; |
| 82 | uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i; |
| 83 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 84 | s->fd = open(filename, O_RDONLY | O_BINARY); |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 85 | if (s->fd < 0) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 86 | return -errno; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 87 | bs->read_only = 1; |
| 88 | s->n_chunks = 0; |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 89 | s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 90 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 91 | /* read offset of info blocks */ |
| 92 | if(lseek(s->fd,-0x1d8,SEEK_END)<0) { |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 93 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 94 | } |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 95 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 96 | info_begin=read_off(s->fd); |
| 97 | if(info_begin==0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 98 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 99 | if(lseek(s->fd,info_begin,SEEK_SET)<0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 100 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 101 | if(read_uint32(s->fd)!=0x100) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 102 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 103 | if((count = read_uint32(s->fd))==0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 104 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 105 | info_end = info_begin+count; |
| 106 | if(lseek(s->fd,0xf8,SEEK_CUR)<0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 107 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 108 | |
| 109 | /* read offsets */ |
| 110 | last_in_offset = last_out_offset = 0; |
| 111 | while(lseek(s->fd,0,SEEK_CUR)<info_end) { |
bellard | 995179f | 2005-01-03 23:39:08 +0000 | [diff] [blame] | 112 | uint32_t type; |
| 113 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 114 | count = read_uint32(s->fd); |
| 115 | if(count==0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 116 | goto fail; |
bellard | 995179f | 2005-01-03 23:39:08 +0000 | [diff] [blame] | 117 | type = read_uint32(s->fd); |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 118 | if(type!=0x6d697368 || count<244) |
| 119 | lseek(s->fd,count-4,SEEK_CUR); |
| 120 | else { |
| 121 | int new_size, chunk_count; |
| 122 | if(lseek(s->fd,200,SEEK_CUR)<0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 123 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 124 | chunk_count = (count-204)/40; |
| 125 | new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count); |
ths | 2137b4c | 2008-08-06 08:37:17 +0000 | [diff] [blame] | 126 | s->types = qemu_realloc(s->types, new_size/2); |
| 127 | s->offsets = qemu_realloc(s->offsets, new_size); |
| 128 | s->lengths = qemu_realloc(s->lengths, new_size); |
| 129 | s->sectors = qemu_realloc(s->sectors, new_size); |
| 130 | s->sectorcounts = qemu_realloc(s->sectorcounts, new_size); |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 131 | |
| 132 | for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) { |
| 133 | s->types[i] = read_uint32(s->fd); |
| 134 | if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) { |
| 135 | if(s->types[i]==0xffffffff) { |
| 136 | last_in_offset = s->offsets[i-1]+s->lengths[i-1]; |
| 137 | last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1]; |
| 138 | } |
| 139 | chunk_count--; |
| 140 | i--; |
| 141 | if(lseek(s->fd,36,SEEK_CUR)<0) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 142 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 143 | continue; |
| 144 | } |
| 145 | read_uint32(s->fd); |
| 146 | s->sectors[i] = last_out_offset+read_off(s->fd); |
| 147 | s->sectorcounts[i] = read_off(s->fd); |
| 148 | s->offsets[i] = last_in_offset+read_off(s->fd); |
| 149 | s->lengths[i] = read_off(s->fd); |
| 150 | if(s->lengths[i]>max_compressed_size) |
| 151 | max_compressed_size = s->lengths[i]; |
| 152 | if(s->sectorcounts[i]>max_sectors_per_chunk) |
| 153 | max_sectors_per_chunk = s->sectorcounts[i]; |
| 154 | } |
| 155 | s->n_chunks+=chunk_count; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* initialize zlib engine */ |
aliguori | 3ec88e8 | 2009-02-05 22:05:53 +0000 | [diff] [blame] | 160 | s->compressed_chunk = qemu_malloc(max_compressed_size+1); |
| 161 | s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk); |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 162 | if(inflateInit(&s->zstream) != Z_OK) |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 163 | goto fail; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 164 | |
| 165 | s->current_chunk = s->n_chunks; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 166 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 167 | return 0; |
Christoph Hellwig | 1559ca0 | 2010-01-11 14:06:54 +0100 | [diff] [blame] | 168 | fail: |
| 169 | close(s->fd); |
| 170 | return -1; |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static inline int is_sector_in_chunk(BDRVDMGState* s, |
| 174 | uint32_t chunk_num,int sector_num) |
| 175 | { |
| 176 | if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num || |
| 177 | s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num) |
| 178 | return 0; |
| 179 | else |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num) |
| 184 | { |
| 185 | /* binary search */ |
| 186 | uint32_t chunk1=0,chunk2=s->n_chunks,chunk3; |
| 187 | while(chunk1!=chunk2) { |
| 188 | chunk3 = (chunk1+chunk2)/2; |
| 189 | if(s->sectors[chunk3]>sector_num) |
| 190 | chunk2 = chunk3; |
| 191 | else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num) |
| 192 | return chunk3; |
| 193 | else |
| 194 | chunk1 = chunk3; |
| 195 | } |
| 196 | return s->n_chunks; /* error */ |
| 197 | } |
| 198 | |
| 199 | static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num) |
| 200 | { |
| 201 | if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) { |
| 202 | int ret; |
| 203 | uint32_t chunk = search_chunk(s,sector_num); |
| 204 | |
| 205 | if(chunk>=s->n_chunks) |
| 206 | return -1; |
| 207 | |
| 208 | s->current_chunk = s->n_chunks; |
| 209 | switch(s->types[chunk]) { |
| 210 | case 0x80000005: { /* zlib compressed */ |
| 211 | int i; |
| 212 | |
| 213 | ret = lseek(s->fd, s->offsets[chunk], SEEK_SET); |
| 214 | if(ret<0) |
| 215 | return -1; |
| 216 | |
| 217 | /* we need to buffer, because only the chunk as whole can be |
| 218 | * inflated. */ |
| 219 | i=0; |
| 220 | do { |
| 221 | ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i); |
| 222 | if(ret<0 && errno==EINTR) |
| 223 | ret=0; |
| 224 | i+=ret; |
| 225 | } while(ret>=0 && ret+i<s->lengths[chunk]); |
| 226 | |
| 227 | if (ret != s->lengths[chunk]) |
| 228 | return -1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 229 | |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 230 | s->zstream.next_in = s->compressed_chunk; |
| 231 | s->zstream.avail_in = s->lengths[chunk]; |
| 232 | s->zstream.next_out = s->uncompressed_chunk; |
| 233 | s->zstream.avail_out = 512*s->sectorcounts[chunk]; |
| 234 | ret = inflateReset(&s->zstream); |
| 235 | if(ret != Z_OK) |
| 236 | return -1; |
| 237 | ret = inflate(&s->zstream, Z_FINISH); |
| 238 | if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk]) |
| 239 | return -1; |
| 240 | break; } |
| 241 | case 1: /* copy */ |
| 242 | ret = read(s->fd, s->uncompressed_chunk, s->lengths[chunk]); |
| 243 | if (ret != s->lengths[chunk]) |
| 244 | return -1; |
| 245 | break; |
| 246 | case 2: /* zero */ |
| 247 | memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]); |
| 248 | break; |
| 249 | } |
| 250 | s->current_chunk = chunk; |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 255 | static int dmg_read(BlockDriverState *bs, int64_t sector_num, |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 256 | uint8_t *buf, int nb_sectors) |
| 257 | { |
| 258 | BDRVDMGState *s = bs->opaque; |
| 259 | int i; |
| 260 | |
| 261 | for(i=0;i<nb_sectors;i++) { |
| 262 | uint32_t sector_offset_in_chunk; |
| 263 | if(dmg_read_chunk(s, sector_num+i) != 0) |
| 264 | return -1; |
| 265 | sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk]; |
| 266 | memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512); |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void dmg_close(BlockDriverState *bs) |
| 272 | { |
| 273 | BDRVDMGState *s = bs->opaque; |
| 274 | close(s->fd); |
| 275 | if(s->n_chunks>0) { |
| 276 | free(s->types); |
| 277 | free(s->offsets); |
| 278 | free(s->lengths); |
| 279 | free(s->sectors); |
| 280 | free(s->sectorcounts); |
| 281 | } |
| 282 | free(s->compressed_chunk); |
| 283 | free(s->uncompressed_chunk); |
| 284 | inflateEnd(&s->zstream); |
| 285 | } |
| 286 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 287 | static BlockDriver bdrv_dmg = { |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 288 | .format_name = "dmg", |
| 289 | .instance_size = sizeof(BDRVDMGState), |
| 290 | .bdrv_probe = dmg_probe, |
| 291 | .bdrv_open = dmg_open, |
| 292 | .bdrv_read = dmg_read, |
| 293 | .bdrv_close = dmg_close, |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 294 | }; |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 295 | |
| 296 | static void bdrv_dmg_init(void) |
| 297 | { |
| 298 | bdrv_register(&bdrv_dmg); |
| 299 | } |
| 300 | |
| 301 | block_init(bdrv_dmg_init); |