File: raw2hdf.c

package info (click to toggle)
fuse-emulator-utils 1.3.1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,480 kB
  • ctags: 1,058
  • sloc: sh: 11,498; ansic: 10,101; cpp: 1,116; makefile: 158
file content (319 lines) | stat: -rw-r--r-- 8,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* raw2hdf.c: Create an .hdf file from a raw binary image
   Copyright (c) 2005 Matthew Westcott, 2006 Philip Kendall
   Copyright (c) 2014-2015 Sergio Baldovi

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   Philip Kendall <philip-fuse@shadowmagic.org.uk>

*/

#include <config.h>

#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ide.h"

#define PROGRAM_NAME "raw2hdf"

const char *progname;

static const char *MODEL_NUMBER = "rCaeet dybr wah2fd                      ";
static const size_t MODEL_NUMBER_LENGTH = 40;

#define CHUNK_LENGTH 1 << 20

static void
show_version( void )
{
  printf(
    PROGRAM_NAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
    "Copyright (c) 2005 Matthew Westcott\n"
    "Copyright (c) 2006 Philip Kendall\n"
    "License GPLv2+: GNU GPL version 2 or later "
    "<http://gnu.org/licenses/gpl.html>\n"
    "This is free software: you are free to change and redistribute it.\n"
    "There is NO WARRANTY, to the extent permitted by law.\n" );
}

static void
show_help( void )
{
  printf(
    "Usage: %s [OPTION] <rawfile> <hdffile>\n"
    "Converts a binary dump of a hard disk's data into an IDE disk image in .hdf \n"
    "format.\n"
    "\n"
    "Options:\n"
    "  -v <version>   Specifies the version of .hdf image to be created (values\n"
    "                   1.0 or 1.1). Defaults to creating version 1.1 files.\n"
    "  -h, --help     Display this help and exit.\n"
    "  -V, --version  Output version information and exit.\n"
    "\n"
    "Report %s bugs to <%s>\n"
    "%s home page: <%s>\n"
    "For complete documentation, see the manual page of %s.\n",
    progname,
    PROGRAM_NAME, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL, PROGRAM_NAME
  );
}

static int
parse_options( int argc, char **argv, const char **raw_filename,
	       const char **hdf_filename, enum hdf_version_t *version )
{
  int c;
  int error = 0;

  struct option long_options[] = {
    { "help", 0, NULL, 'h' },
    { "version", 0, NULL, 'V' },
    { 0, 0, 0, 0 }
  };

  while( ( c = getopt_long( argc, argv, "v:hV", long_options, NULL ) ) != -1 ) {

    switch( c ) {

    case 'v':
      if( strcasecmp( optarg, "1.0" ) == 0 ) {
	*version = HDF_VERSION_10;
      } else if( strcasecmp( optarg, "1.1" ) == 0 ) {
	*version = HDF_VERSION_11;
      } else {
        error = 1;
        fprintf( stderr, "%s: version not supported\n", progname );
      }
      break;

    case 'h':
      show_help();
      exit( 0 );

    case 'V':
      show_version();
      exit( 0 );

    case '?':
      /* getopt prints an error message to stderr */
      error = 1;
      break;

    default:
      error = 1;
      fprintf( stderr, "%s: unknown option `%c'\n", progname, (char) c );
      break;

    }
  }
  argc -= optind;
  argv += optind;

  if( error ) return error;

  if( argc != 2 ) {
    fprintf( stderr, "%s: usage: %s [-v<version>] <raw-filename> <hdf-filename>\n",
             progname, progname );
    return 1;
  }

  *raw_filename  = argv[0];
  *hdf_filename  = argv[1];

  return 0;
}

static int
write_header( FILE *f, size_t byte_count, const char *filename,
	      enum hdf_version_t version )
{

  char *hdf_header, *identity;
  size_t bytes, total_sectors, head_count, cyl_count, sector_count;
  size_t sectors_per_head, data_offset;

  data_offset = hdf_data_offset( version );

  hdf_header = calloc( 1, data_offset );
  if( !hdf_header ) {
    fprintf( stderr, "%s: out of memory at line %d\n", progname, __LINE__ );
    return 1;
  }

  memcpy( &hdf_header[ HDF_SIGNATURE_OFFSET ], HDF_SIGNATURE,
          HDF_SIGNATURE_LENGTH );
  hdf_header[ HDF_VERSION_OFFSET ] = hdf_version( version );
  hdf_header[ HDF_COMPACT_OFFSET ] = 0;
  hdf_header[ HDF_DATA_OFFSET_OFFSET     ] = ( data_offset      ) & 0xff;
  hdf_header[ HDF_DATA_OFFSET_OFFSET + 1 ] = ( data_offset >> 8 ) & 0xff;

  identity = &hdf_header[ HDF_IDENTITY_OFFSET ];

  /* invent suitable geometry to match the file length */
  if( byte_count & 0x1f ) {
    fprintf( stderr,
	     "%s: warning: image is not a whole number of sectors in length\n",
	     progname );
  }
  total_sectors = byte_count >> 9;
  if( total_sectors >= 16514064 ) {
    /* image > 8GB; use dummy 'large disk' CHS values */
    cyl_count = 16383;
    head_count = 16;
    sector_count = 63;
  } else {
    /* find largest factor <= 16 to use as the head count */
    for( head_count = 16; head_count > 1; head_count-- ) {
      if( total_sectors % head_count == 0 ) break;
    }
    sectors_per_head = total_sectors / head_count;
    /* find largest factor <= 63 to use as the sector count */
    for( sector_count = 63; sector_count > 1; sector_count-- ) {
      if( sectors_per_head % sector_count == 0 ) break;
    }
    cyl_count = sectors_per_head / sector_count;
    if( cyl_count > 16384 ) {
      /* attempt to factorise into sensible CHS values failed dismally;
         fall back on dummy 'large disk' values */
      cyl_count = 16383;
      head_count = 16;
      sector_count = 63;
    }
  }

  identity[ IDENTITY_CYLINDERS_OFFSET     ] = ( cyl_count      ) & 0xff;
  identity[ IDENTITY_CYLINDERS_OFFSET + 1 ] = ( cyl_count >> 8 ) & 0xff;

  identity[ IDENTITY_HEADS_OFFSET     ] = ( head_count      ) & 0xff;
  identity[ IDENTITY_HEADS_OFFSET + 1 ] = ( head_count >> 8 ) & 0xff;

  identity[ IDENTITY_SECTORS_OFFSET     ] = ( sector_count      ) & 0xff;
  identity[ IDENTITY_SECTORS_OFFSET + 1 ] = ( sector_count >> 8 ) & 0xff;

  /* set 'LBA supported' flag */
  identity[ IDENTITY_CAPABILITIES_OFFSET + 1 ] = 0x02;

  memcpy( &identity[ IDENTITY_MODEL_NUMBER_OFFSET ], MODEL_NUMBER,
          MODEL_NUMBER_LENGTH );  

  rewind( f );
  bytes = fwrite( hdf_header, 1, data_offset, f );
  if( bytes != data_offset ) {
    fprintf( stderr,
             "%s: could write only %lu header bytes out of %lu to '%s'\n",
             progname, (unsigned long)bytes, (unsigned long)data_offset,
             filename );
    free( hdf_header );
    return 1;
  }

  free( hdf_header );

  return 0;
}

static int
copy_data( FILE *from, FILE *to, size_t *byte_count, const char *from_filename,
	   const char *to_filename, enum hdf_version_t version )
{
  char buffer[ CHUNK_LENGTH ];
  size_t bytes_read;

  if( fseek( to, hdf_data_offset( version ), SEEK_SET ) ) {
    fprintf( stderr, "%s: error seeking in '%s': %s\n", progname,
             to_filename, strerror( errno ) );
    return 1;
  }
  
  *byte_count = 0;
  
  while( !feof( from ) ) {
    bytes_read = fread( buffer, 1, CHUNK_LENGTH, from );
    if( ferror( from ) ) {
      fprintf( stderr, "%s: error reading from '%s': %s\n", progname,
               from_filename, strerror( errno ) );
      return 1;
    }
    *byte_count += bytes_read;
    
    fwrite( buffer, 1, bytes_read, to );
    if( ferror( to ) ) {
      fprintf( stderr, "%s: error writing to '%s': %s\n", progname,
               to_filename, strerror( errno ) );
      return 1;
    }
  }

  return 0;
}

int
main( int argc, char **argv )
{
  const char *raw_filename, *hdf_filename;
  FILE *raw, *hdf;
  int error;
  size_t byte_count;
  enum hdf_version_t version = HDF_VERSION_11;

  progname = argv[0];

  error = parse_options( argc, argv, &raw_filename, &hdf_filename, &version );
  if( error ) {
    fprintf( stderr, "Try `%s --help' for more information.\n", progname );
    return error;
  }

  raw = fopen( raw_filename, "rb" );
  if( !raw ) {
    fprintf( stderr, "%s: error opening '%s': %s\n", progname, raw_filename,
	     strerror( errno ) );
    return 1;
  }

  hdf = fopen( hdf_filename, "wb" );
  if( !hdf ) {
    fprintf( stderr, "%s: error opening '%s': %s\n", progname, hdf_filename,
	     strerror( errno ) );
    fclose( raw );
    return 1;
  }

  error = copy_data( raw, hdf, &byte_count, raw_filename, hdf_filename,
		     version );
  if( error ) {
    fclose( hdf );
    fclose( raw );
    return error;
  }

  fclose( raw );

  error = write_header( hdf, byte_count, hdf_filename, version );
  if( error ) {
    fclose( hdf );
    return error;
  }

  fclose( hdf );

  return 0;
}