Search
lxdream.org :: lxdream/src/vmu/vmuvol.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/vmu/vmuvol.c
changeset 1296:30ecee61f811
prev1071:182cfe43c09e
author nkeynes
date Fri May 29 18:47:05 2015 +1000 (8 years ago)
permissions -rw-r--r--
last change Fix test case
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * VMU volume (ie block device) support
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <glib.h>
    20 #include <string.h>
    21 #include <unistd.h>
    22 #include <stdio.h>
    23 #include <fcntl.h>
    24 #include <errno.h>
    26 #include "vmu/vmuvol.h"
    27 #include "dream.h"
    28 #include "lxpaths.h"
    30 #define VMU_MAX_PARTITIONS 256
    31 #define VMU_MAX_BLOCKS 65536 /* Actually slightly less than this, but it'll do */
    33 typedef struct vmu_partition {
    34     struct vmu_volume_metadata metadata;
    35     uint32_t block_count;
    36     char *blocks;
    37 } *vmu_partition_t;
    39 struct vmu_volume {
    40     const gchar *display_name;
    41     vmu_partnum_t part_count;
    42     gboolean dirty;
    43     struct vmu_partition part[0];
    44 };
    46 /* On-VMU structures, courtesy of Marcus Comstedt */ 
    47 struct vmu_superblock {
    48     char magic[16];
    49     uint8_t colour_flag;
    50     uint8_t bgra[4];
    51     uint8_t pad1[27];
    52     char timestamp[8];
    53     char pad2[8];
    54     char unknown[6];
    55     uint16_t fat_block;
    56     uint16_t fat_size;
    57     uint16_t dir_block;
    58     uint16_t dir_size;
    59     uint16_t icon_shape;
    60     uint16_t user_size;
    61     /* remainder unknown */
    62 };
    64 struct vmu_direntry {
    65     uint8_t filetype;
    66     uint8_t copy_flag;
    67     uint16_t blkno;
    68     char filename[12];
    69     char timestamp[8];
    70     uint16_t blksize; /* Size in blocks*/
    71     uint16_t hdroff; /* Header offset in blocks */
    72     char pad[4];
    73 };
    75 #define MD(vmu,ptno) ((vmu)->part[ptno].metadata)
    77 #define VMU_BLOCK(vmu,ptno,blkno) (&(vmu)->part[ptno].blocks[(blkno)*VMU_BLOCK_SIZE])
    79 #define VMU_FAT_ENTRY(vmu,pt,ent)  ((uint16_t *)VMU_BLOCK(vmu, pt, (MD(vmu,pt).fat_block - ((ent)>>8))))[(ent)&0xFF]
    81 #define FAT_EMPTY 0xFFFC
    82 #define FAT_EOF   0xFFFA
    84 static const struct vmu_volume_metadata default_metadata = { 255, 255, 254, 1, 253, 13, 0, 200, {31, 0, 128} };
    86 vmu_volume_t vmu_volume_new_default( const gchar *display_name )
    87 {
    88     vmu_volume_t vol = g_malloc0( sizeof(struct vmu_volume) + sizeof(struct vmu_partition) );
    89     vol->part_count = 1;
    90     vol->dirty = FALSE;
    91     memcpy( &vol->part[0].metadata, &default_metadata, sizeof(struct vmu_volume_metadata) );
    92     vol->part[0].block_count = VMU_DEFAULT_VOL_BLOCKS;
    93     vol->part[0].blocks = g_malloc0( VMU_DEFAULT_VOL_BLOCKS * VMU_BLOCK_SIZE );
    94     vol->display_name = display_name == NULL ? NULL : g_strdup(display_name);
    95     vmu_volume_format( vol, 0, TRUE );
    96     return vol;
    97 }
    99 void vmu_volume_destroy( vmu_volume_t vol )
   100 {
   101     int i;
   102     if( vol == NULL )
   103         return;
   105     for( i=0; i<vol->part_count; i++ ) {
   106         g_free( vol->part[i].blocks );
   107         vol->part[i].blocks = NULL;
   108     }
   109     if( vol->display_name ) {
   110         g_free( (char *)vol->display_name );
   111         vol->display_name = NULL;
   112     }
   113     g_free(vol);
   114 }
   116 void vmu_volume_format( vmu_volume_t vol, vmu_partnum_t pt, gboolean quick )
   117 {
   118     if( pt >= vol->part_count ) {
   119         return;
   120     }
   122     if( !quick ) {
   123         /* Wipe it completely first */
   124         memset( vol->part[pt].blocks, 0, (vol->part[pt].block_count) * VMU_BLOCK_SIZE );
   125     }
   127     struct vmu_volume_metadata *meta = &vol->part[pt].metadata;
   128     unsigned int fatblkno = meta->fat_block;
   129     unsigned int dirblkno = meta->dir_block;
   131     /* Write superblock */
   132     struct vmu_superblock *super = (struct vmu_superblock *)VMU_BLOCK(vol,pt, meta->super_block);
   133     memset( super->magic, 0x55, 16 );
   134     memset( &super->colour_flag, 0, 240 ); /* Blank the rest for now */
   135     super->fat_block = meta->fat_block;
   136     super->fat_size = meta->fat_size;
   137     super->dir_block = meta->dir_block;
   138     super->user_size = meta->user_size;
   140     /* Write file allocation tables */
   141     int i,j;
   142     for( j=0; j<meta->fat_size; j++ ) {
   143         uint16_t *fat = (uint16_t *)VMU_BLOCK(vol,pt,fatblkno-j); 
   144         for( i=0; i<256; i++ ) {
   145             fat[i] = FAT_EMPTY;
   146         }
   147     }
   149     /* Fill in the system allocations in the FAT */
   150     for( i=0; i<meta->fat_size-1; i++ ) {
   151         VMU_FAT_ENTRY(vol,pt,fatblkno-i) = fatblkno-i-1;
   152     }
   153     VMU_FAT_ENTRY(vol,pt,fatblkno - i) = FAT_EOF;
   154     for( i=0; i<meta->dir_size-1; i++ ) {
   155         VMU_FAT_ENTRY(vol,pt,dirblkno-i) = dirblkno-i-1;
   156     }
   157     VMU_FAT_ENTRY(vol,pt,dirblkno-i) = FAT_EOF;
   159     /* If quick-format, blank the directory. Otherwise it's already been done */
   160     if( quick ) {
   161         memset( VMU_BLOCK(vol,pt,dirblkno-meta->dir_size+1),
   162                 0, meta->dir_size * VMU_BLOCK_SIZE );
   163     }
   164 }
   166 /*************************** File load/save ********************************/
   168 /**
   169  * Current file has 1 META chunk for all volume metadata, followed by a
   170  * DATA chunk for each partition's block data. The META chunk is required to
   171  * occur before any DATA blocks.
   172  * Unknown chunks are skipped to allow for forwards compatibility if/when
   173  * we add the VMU runtime side of things
   174  */
   176 struct vmu_file_header {
   177     char magic[16];
   178     uint32_t version;
   179     uint32_t head_len;
   180     uint32_t part_count;
   181     uint32_t display_name_len;
   182     char display_name[0];
   183 };
   185 struct vmu_chunk_header {
   186     char name[4];
   187     uint32_t length;
   188 };
   191 gboolean vmu_volume_save( const gchar *filename, vmu_volume_t vol, gboolean create_only )
   192 {
   193     struct vmu_file_header head;
   194     struct vmu_chunk_header chunk;
   195     int i;
   197     gchar *tempfile = get_filename_at(filename, ".XXXXXXX");
   198     int fd = mkstemp( tempfile );
   199     if( fd == -1 ) {
   200         g_free(tempfile);
   201         return FALSE;
   202     }
   204     FILE *f = fdopen( fd, "w+" );
   207     /* File header */
   208     memcpy( head.magic, VMU_FILE_MAGIC, 16 );
   209     head.version = VMU_FILE_VERSION;
   210     head.part_count = vol->part_count;
   211     head.display_name_len = vol->display_name == NULL ? 0 : (strlen(vol->display_name)+1);
   212     head.head_len = sizeof(head) + head.display_name_len;
   213     fwrite( &head, sizeof(head), 1, f );
   214     if( vol->display_name != NULL ) {
   215         fwrite( vol->display_name, head.display_name_len, 1, f );
   216     }
   218     /* METAdata chunk */
   219     memcpy( chunk.name, "META", 4 );
   220     chunk.length = sizeof(struct vmu_volume_metadata) * vol->part_count;
   221     fwrite( &chunk, sizeof(chunk), 1, f );
   222     for( i=0; i < vol->part_count; i++ ) {
   223         fwrite( &vol->part[i].metadata, sizeof(struct vmu_volume_metadata), 1, f );
   224     }
   226     /* partition DATA chunks */
   227     for( i=0; i< vol->part_count; i++ ) {
   228         memcpy( chunk.name, "DATA", 4 );
   229         chunk.length = 0;
   230         if( fwrite( &chunk, sizeof(chunk), 1, f ) != 1 ) goto cleanup;
   231         long posn = ftell(f);
   232         if( fwrite( &vol->part[i].block_count, sizeof(vol->part[i].block_count), 1, f ) != 1 ) goto cleanup;
   233         fwrite_gzip( vol->part[i].blocks, vol->part[i].block_count, VMU_BLOCK_SIZE, f );
   234         long end = ftell(f);
   235         fseek( f, posn - sizeof(chunk.length), SEEK_SET );
   236         chunk.length = end-posn;
   237         if( fwrite( &chunk.length, sizeof(chunk.length), 1, f ) != 1 ) goto cleanup;
   238         fseek( f, end, SEEK_SET );
   239     }
   240     fclose(f);
   241     f = NULL;
   243     if( rename(tempfile, filename) != 0 )
   244         goto cleanup;
   246     /* All good */
   247     vol->dirty = FALSE;
   248     g_free(tempfile);
   249     return TRUE;
   251 cleanup:
   252     if( f != NULL )
   253         fclose(f);
   254     unlink(tempfile);
   255     g_free(tempfile);
   256     return FALSE;
   257 }
   259 vmu_volume_t vmu_volume_load( const gchar *filename )
   260 {
   261     struct vmu_file_header head;
   262     struct vmu_chunk_header chunk;
   263     vmu_volume_t vol;
   264     int i;
   266     FILE *f = fopen( filename, "ro" );
   267     if( f == NULL ) {
   268         ERROR( "Unable to open VMU file '%s': %s", filename, strerror(errno) );
   269         return FALSE;
   270     }
   272     if( fread( &head, sizeof(head), 1, f ) != 1 ||
   273         memcmp(head.magic, VMU_FILE_MAGIC, 16) != 0 ||
   274         head.part_count > VMU_MAX_PARTITIONS || 
   275         head.head_len < (sizeof(head) + head.display_name_len) )  {
   276         fclose(f);
   277         ERROR( "Unable to load VMU '%s': bad file header", filename );
   278         return NULL;
   279     }
   281     vol = (vmu_volume_t)g_malloc0( sizeof(struct vmu_volume) + sizeof(struct vmu_partition)*head.part_count );
   282     vol->part_count = head.part_count;
   283     vol->dirty = FALSE;
   284     if( head.display_name_len != 0 ) {
   285         vol->display_name = g_malloc( head.display_name_len );
   286         fread( (char *)vol->display_name, head.display_name_len, 1, f );
   287     }
   288     fseek( f, head.head_len, SEEK_SET );
   290     gboolean have_meta = FALSE;
   291     int next_part = 0;
   292     while( !feof(f) && fread( &chunk, sizeof(chunk), 1, f ) == 1 ) {
   293         if( memcmp( &chunk.name, "META", 4 ) == 0 ) {
   294             if( have_meta || chunk.length != head.part_count * sizeof(struct vmu_volume_metadata) ) {
   295                 vmu_volume_destroy(vol);
   296                 fclose(f);
   297                 ERROR( "Unable to load VMU '%s': bad metadata size (expected %d but was %d)", filename,
   298                        head.part_count * sizeof(struct vmu_volume_metadata), chunk.length );
   299                 return NULL;
   300             }
   301             for( i=0; i<head.part_count; i++ ) {
   302                 fread( &vol->part[i].metadata, sizeof(struct vmu_volume_metadata), 1, f );
   303             }
   304             have_meta = TRUE;
   305         } else if( memcmp( &chunk.name, "DATA", 4 ) == 0 ) {
   306             uint32_t block_count;
   307             fread( &block_count, sizeof(block_count), 1, f );
   308             if( next_part >= vol->part_count || block_count >= VMU_MAX_BLOCKS ) {
   309                 // Too many partitions / blocks
   310                 vmu_volume_destroy(vol);
   311                 fclose(f);
   312                 ERROR( "Unable to load VMU '%s': too large (%d/%d)", filename, next_part, block_count );
   313                 return NULL;
   314             }
   315             vol->part[next_part].block_count = block_count;
   316             vol->part[next_part].blocks = g_malloc(block_count*VMU_BLOCK_SIZE);
   317             fread_gzip(vol->part[next_part].blocks, VMU_BLOCK_SIZE, block_count, f );
   318             next_part++;
   319         } else {
   320             // else skip unknown block
   321             fseek( f, SEEK_CUR, chunk.length );
   322             WARN( "Unexpected VMU data chunk: '%4.4s'", chunk.name );
   323         }
   324     }
   326     fclose(f);
   328     if( !have_meta || next_part != vol->part_count ) {
   329         vmu_volume_destroy( vol );
   330         return NULL;
   331     }
   333     return vol;
   334 }
   336 /*************************** Accessing data ********************************/
   337 const char *vmu_volume_get_display_name( vmu_volume_t vol ) 
   338 {
   339     return vol->display_name;
   340 }
   342 void vmu_volume_set_display_name( vmu_volume_t vol, const gchar *name )
   343 {
   344     if( vol->display_name != NULL ) {
   345         g_free( (char *)vol->display_name );
   346     }
   347     if( name == NULL ) {
   348         vol->display_name = NULL;
   349     } else {
   350         vol->display_name = g_strdup(name);
   351     }
   352 }
   354 gboolean vmu_volume_is_dirty( vmu_volume_t vol )
   355 {
   356     return vol->dirty;
   357 }
   359 gboolean vmu_volume_read_block( vmu_volume_t vol, vmu_partnum_t pt, unsigned int block, unsigned char *out )
   360 {
   361     if( pt >= vol->part_count || block >= vol->part[pt].block_count ) {
   362         return FALSE;
   363     }
   365     memcpy( out, VMU_BLOCK(vol,pt,block), VMU_BLOCK_SIZE );
   366     return TRUE;
   367 }
   369 gboolean vmu_volume_write_block( vmu_volume_t vol, vmu_partnum_t pt, unsigned int block, unsigned char *in )
   370 {
   371     if( pt >= vol->part_count || block >= vol->part[pt].block_count ) {
   372         return FALSE;
   373     }
   374     memcpy( VMU_BLOCK(vol,pt,block), in, VMU_BLOCK_SIZE );
   375     vol->dirty = TRUE;
   376     return TRUE;
   377 }
   379 gboolean vmu_volume_write_phase( vmu_volume_t vol, vmu_partnum_t pt, unsigned int block, unsigned int phase, unsigned char *in )
   380 {
   381     if( pt >= vol->part_count || block >= vol->part[pt].block_count || phase >= 4 ) {
   382         return FALSE;
   383     }
   384     memcpy( VMU_BLOCK(vol,pt,block) + (phase*128), in, VMU_BLOCK_SIZE/4 );
   385     vol->dirty = TRUE;
   386     return TRUE;
   387 }
   389 const struct vmu_volume_metadata *vmu_volume_get_metadata( vmu_volume_t vol, vmu_partnum_t partition )
   390 {
   391     if( partition >= vol->part_count ) {
   392         return NULL;
   393     } else {
   394         return &vol->part[partition].metadata;
   395     }
   396 }
.