]> Git Repo - qemu.git/blobdiff - block/dmg.c
Update Makefile to actually install the new efi-enabled nic roms that are used by...
[qemu.git] / block / dmg.c
index a0ba34fb8991a4c3e59f0c9ac51c7f2cbbc3ddd5..3141cb5b889a4972eb40a3e09e761cac85db2bfa 100644 (file)
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#include "block_int.h"
-#include "bswap.h"
-#include "module.h"
+#include "block/block_int.h"
+#include "qemu/bswap.h"
+#include "qemu/module.h"
 #include <zlib.h>
 
 typedef struct BDRVDMGState {
-    int fd;
-
+    CoMutex lock;
     /* each chunk contains a certain number of sectors,
      * offsets[i] is the offset in the .dmg file,
      * lengths[i] is the length of the compressed chunk,
@@ -52,60 +51,89 @@ typedef struct BDRVDMGState {
 
 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
 {
-    int len=strlen(filename);
-    if(len>4 && !strcmp(filename+len-4,".dmg"))
-       return 2;
+    int len;
+
+    if (!filename) {
+        return 0;
+    }
+
+    len = strlen(filename);
+    if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
+        return 2;
+    }
     return 0;
 }
 
-static off_t read_off(int fd, int64_t offset)
+static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
 {
-       uint64_t buffer;
-       if (pread(fd, &buffer, 8, offset) < 8)
-               return 0;
-       return be64_to_cpu(buffer);
+    uint64_t buffer;
+    int ret;
+
+    ret = bdrv_pread(bs->file, offset, &buffer, 8);
+    if (ret < 0) {
+        return ret;
+    }
+
+    *result = be64_to_cpu(buffer);
+    return 0;
 }
 
-static off_t read_uint32(int fd, int64_t offset)
+static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
 {
-       uint32_t buffer;
-       if (pread(fd, &buffer, 4, offset) < 4)
-               return 0;
-       return be32_to_cpu(buffer);
+    uint32_t buffer;
+    int ret;
+
+    ret = bdrv_pread(bs->file, offset, &buffer, 4);
+    if (ret < 0) {
+        return ret;
+    }
+
+    *result = be32_to_cpu(buffer);
+    return 0;
 }
 
-static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
+static int dmg_open(BlockDriverState *bs, QDict *options, int flags)
 {
     BDRVDMGState *s = bs->opaque;
-    off_t info_begin,info_end,last_in_offset,last_out_offset;
-    uint32_t count;
+    uint64_t info_begin,info_end,last_in_offset,last_out_offset;
+    uint32_t count, tmp;
     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
     int64_t offset;
+    int ret;
 
-    s->fd = open(filename, O_RDONLY | O_BINARY);
-    if (s->fd < 0)
-        return -errno;
     bs->read_only = 1;
     s->n_chunks = 0;
     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
 
     /* read offset of info blocks */
-    offset = lseek(s->fd, -0x1d8, SEEK_END);
+    offset = bdrv_getlength(bs->file);
     if (offset < 0) {
+        ret = offset;
         goto fail;
     }
+    offset -= 0x1d8;
 
-    info_begin = read_off(s->fd, offset);
-    if (info_begin == 0) {
-       goto fail;
+    ret = read_uint64(bs, offset, &info_begin);
+    if (ret < 0) {
+        goto fail;
+    } else if (info_begin == 0) {
+        ret = -EINVAL;
+        goto fail;
     }
 
-    if (read_uint32(s->fd, info_begin) != 0x100) {
+    ret = read_uint32(bs, info_begin, &tmp);
+    if (ret < 0) {
+        goto fail;
+    } else if (tmp != 0x100) {
+        ret = -EINVAL;
         goto fail;
     }
 
-    count = read_uint32(s->fd, info_begin + 4);
-    if (count == 0) {
+    ret = read_uint32(bs, info_begin + 4, &count);
+    if (ret < 0) {
+        goto fail;
+    } else if (count == 0) {
+        ret = -EINVAL;
         goto fail;
     }
     info_end = info_begin + count;
@@ -117,12 +145,20 @@ static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
     while (offset < info_end) {
         uint32_t type;
 
-       count = read_uint32(s->fd, offset);
-       if(count==0)
-           goto fail;
+        ret = read_uint32(bs, offset, &count);
+        if (ret < 0) {
+            goto fail;
+        } else if (count == 0) {
+            ret = -EINVAL;
+            goto fail;
+        }
         offset += 4;
 
-       type = read_uint32(s->fd, offset);
+        ret = read_uint32(bs, offset, &type);
+        if (ret < 0) {
+            goto fail;
+        }
+
        if (type == 0x6d697368 && count >= 244) {
            int new_size, chunk_count;
 
@@ -131,14 +167,17 @@ static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
 
            chunk_count = (count-204)/40;
            new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
-           s->types = qemu_realloc(s->types, new_size/2);
-           s->offsets = qemu_realloc(s->offsets, new_size);
-           s->lengths = qemu_realloc(s->lengths, new_size);
-           s->sectors = qemu_realloc(s->sectors, new_size);
-           s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
-
-           for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
-               s->types[i] = read_uint32(s->fd, offset);
+           s->types = g_realloc(s->types, new_size/2);
+           s->offsets = g_realloc(s->offsets, new_size);
+           s->lengths = g_realloc(s->lengths, new_size);
+           s->sectors = g_realloc(s->sectors, new_size);
+           s->sectorcounts = g_realloc(s->sectorcounts, new_size);
+
+            for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
+                ret = read_uint32(bs, offset, &s->types[i]);
+                if (ret < 0) {
+                    goto fail;
+                }
                offset += 4;
                if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
                    if(s->types[i]==0xffffffff) {
@@ -152,17 +191,31 @@ static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
                }
                offset += 4;
 
-               s->sectors[i] = last_out_offset+read_off(s->fd, offset);
-               offset += 8;
-
-               s->sectorcounts[i] = read_off(s->fd, offset);
-               offset += 8;
-
-               s->offsets[i] = last_in_offset+read_off(s->fd, offset);
-               offset += 8;
-
-               s->lengths[i] = read_off(s->fd, offset);
-               offset += 8;
+                ret = read_uint64(bs, offset, &s->sectors[i]);
+                if (ret < 0) {
+                    goto fail;
+                }
+                s->sectors[i] += last_out_offset;
+                offset += 8;
+
+                ret = read_uint64(bs, offset, &s->sectorcounts[i]);
+                if (ret < 0) {
+                    goto fail;
+                }
+                offset += 8;
+
+                ret = read_uint64(bs, offset, &s->offsets[i]);
+                if (ret < 0) {
+                    goto fail;
+                }
+                s->offsets[i] += last_in_offset;
+                offset += 8;
+
+                ret = read_uint64(bs, offset, &s->lengths[i]);
+                if (ret < 0) {
+                    goto fail;
+                }
+                offset += 8;
 
                if(s->lengths[i]>max_compressed_size)
                    max_compressed_size = s->lengths[i];
@@ -174,17 +227,27 @@ static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
     }
 
     /* initialize zlib engine */
-    s->compressed_chunk = qemu_malloc(max_compressed_size+1);
-    s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk);
-    if(inflateInit(&s->zstream) != Z_OK)
-       goto fail;
+    s->compressed_chunk = g_malloc(max_compressed_size+1);
+    s->uncompressed_chunk = g_malloc(512*max_sectors_per_chunk);
+    if(inflateInit(&s->zstream) != Z_OK) {
+        ret = -EINVAL;
+        goto fail;
+    }
 
     s->current_chunk = s->n_chunks;
 
+    qemu_co_mutex_init(&s->lock);
     return 0;
+
 fail:
-    close(s->fd);
-    return -1;
+    g_free(s->types);
+    g_free(s->offsets);
+    g_free(s->lengths);
+    g_free(s->sectors);
+    g_free(s->sectorcounts);
+    g_free(s->compressed_chunk);
+    g_free(s->uncompressed_chunk);
+    return ret;
 }
 
 static inline int is_sector_in_chunk(BDRVDMGState* s,
@@ -213,8 +276,10 @@ static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
     return s->n_chunks; /* error */
 }
 
-static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
+static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
 {
+    BDRVDMGState *s = bs->opaque;
+
     if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
        int ret;
        uint32_t chunk = search_chunk(s,sector_num);
@@ -231,8 +296,8 @@ static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
             * inflated. */
            i=0;
            do {
-               ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
-                            s->offsets[chunk] + i);
+                ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
+                                 s->compressed_chunk+i, s->lengths[chunk]-i);
                if(ret<0 && errno==EINTR)
                    ret=0;
                i+=ret;
@@ -253,8 +318,8 @@ static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
                return -1;
            break; }
        case 1: /* copy */
-           ret = pread(s->fd, s->uncompressed_chunk, s->lengths[chunk],
-                        s->offsets[chunk]);
+           ret = bdrv_pread(bs->file, s->offsets[chunk],
+                             s->uncompressed_chunk, s->lengths[chunk]);
            if (ret != s->lengths[chunk])
                return -1;
            break;
@@ -275,7 +340,7 @@ static int dmg_read(BlockDriverState *bs, int64_t sector_num,
 
     for(i=0;i<nb_sectors;i++) {
        uint32_t sector_offset_in_chunk;
-       if(dmg_read_chunk(s, sector_num+i) != 0)
+       if(dmg_read_chunk(bs, sector_num+i) != 0)
            return -1;
        sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
        memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
@@ -283,19 +348,29 @@ static int dmg_read(BlockDriverState *bs, int64_t sector_num,
     return 0;
 }
 
+static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
+                                    uint8_t *buf, int nb_sectors)
+{
+    int ret;
+    BDRVDMGState *s = bs->opaque;
+    qemu_co_mutex_lock(&s->lock);
+    ret = dmg_read(bs, sector_num, buf, nb_sectors);
+    qemu_co_mutex_unlock(&s->lock);
+    return ret;
+}
+
 static void dmg_close(BlockDriverState *bs)
 {
     BDRVDMGState *s = bs->opaque;
-    close(s->fd);
-    if(s->n_chunks>0) {
-       free(s->types);
-       free(s->offsets);
-       free(s->lengths);
-       free(s->sectors);
-       free(s->sectorcounts);
-    }
-    free(s->compressed_chunk);
-    free(s->uncompressed_chunk);
+
+    g_free(s->types);
+    g_free(s->offsets);
+    g_free(s->lengths);
+    g_free(s->sectors);
+    g_free(s->sectorcounts);
+    g_free(s->compressed_chunk);
+    g_free(s->uncompressed_chunk);
+
     inflateEnd(&s->zstream);
 }
 
@@ -303,8 +378,8 @@ static BlockDriver bdrv_dmg = {
     .format_name       = "dmg",
     .instance_size     = sizeof(BDRVDMGState),
     .bdrv_probe                = dmg_probe,
-    .bdrv_file_open    = dmg_open,
-    .bdrv_read         = dmg_read,
+    .bdrv_open         = dmg_open,
+    .bdrv_read          = dmg_co_read,
     .bdrv_close                = dmg_close,
 };
 
This page took 0.03608 seconds and 4 git commands to generate.