summaryrefslogtreecommitdiffstats
path: root/getgeo.c
blob: 2a17a65e66bed8075ad99a08bb55457ea082a6c5 (plain)
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
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2002 H. Peter Anvin - All Rights Reserved
 *
 *   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, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * getgeo.c
 *
 * Obtain output image length and/or geometry
 */

#include <inttypes.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <linux/fs.h>
#include <linux/fd.h>
#include <stdio.h>
#include "geo.h"

extern const char *program;

int
get_geometry(int fd, struct image_geometry *geo)
{
  struct hd_geometry hdgeo;
  struct floppy_struct fdgeo;
  struct stat st;
  uint64_t blksize64;
  unsigned long blksize;
  unsigned int secsize;
  const struct image_geometry *sg;
  const struct image_geometry standard_geometries[] = {
    { 40, 1,  8, 0xfe,  160<<10, 0, 9 }, /*  160K 5.25" SSDD standard */
    { 40, 1,  9, 0xfc,  180<<10, 0, 9 }, /*  180K 5.25" SSDD standard */
    { 40, 2,  8, 0xff,  320<<10, 0, 9 }, /*  320K 5.25" DSDD standard */
    { 40, 2,  9, 0xfd,  360<<10, 0, 9 }, /*  360K 5.25" DSDD standard */
    { 80, 2,  9, 0xf9,  720<<10, 0, 9 }, /*  720K 3.5"  DSDD standard */
    { 80, 2, 15, 0xf9, 1200<<10, 0, 9 }, /* 1200K 5.25" DSHD standard */
    { 80, 2, 18, 0xf0, 1440<<10, 0, 9 }, /* 1440K 3.5"  DSHD standard */
    { 80, 2, 21, 0xf0, 1680<<10, 0, 9 }, /* 1680K 3.5"  DSHD extended */
    { 82, 2, 21, 0xf0, 1722<<10, 0, 9 }, /* 1722K 3.5"  DSHD extended */
    { 80, 2, 36, 0xf0, 2880<<10, 0, 9 }, /* 2880K 3.5"  DSED standard */
    { 0, 0, 0, 0, 0, 0, 0 }
  };

  if ( fstat(fd, &st) )
    return -1;
  
  memset(geo, 0, sizeof(*geo));

  if ( S_ISBLK(st.st_mode) ) {
#ifdef BLKGETSIZE64
    if ( !ioctl(fd, BLKGETSIZE64, &blksize64) ) {
      geo->size = blksize64;
    } else 
#endif
      if ( !ioctl(fd, BLKGETSIZE, &blksize) ) {
	geo->size = (uint64_t)blksize << 9;
      }
    if ( !ioctl(fd, BLKSSZGET, &secsize) ) {
      while ( secsize > 1 ) {
	geo->secshift++;
	secsize >>= 1;
      }
    }

    if ( !ioctl(fd, FDGETPRM, &fdgeo) ) {
      geo->c = fdgeo.track;	/* IS THIS CORRECT?!? */
      geo->h = fdgeo.head;
      geo->s = fdgeo.sect;
      geo->idbyte = 0xf0;	/* Floppy disk "other" */
    } else if ( !ioctl(fd, HDIO_GETGEO, &hdgeo) ) {
      /* Got the geometry */
      geo->c = hdgeo.cylinders; /* Don't use... may be truncated */
      geo->h = hdgeo.heads;
      geo->s = hdgeo.sectors;
      geo->idbyte = 0xf8;	/* Hard disk */
      geo->offset = (uint64_t)hdgeo.start << geo->secshift;
    }
      
    /* If it's one of the standard floppy geometries set the ID byte correctly */
    for ( sg = standard_geometries ; sg->size ; sg++ ) {
      if ( sg->c == geo->c && sg->h == geo->h && sg->s == geo->s ) {
	geo->idbyte = sg->idbyte;
	break;
      }
    }
  } else if ( S_ISREG(st.st_mode) ) {
    /* Plain file; set the size and hunt for a standard geometry, otherwise
       just make something up */
      geo->size = st.st_size;

      for ( sg = standard_geometries ; sg->size ; sg++ ) {
	if ( sg->size == geo->size ) {
	  *geo = *sg;
	  return 0;
	}
      }
      /* FIX: Assume it's a hard disk and make something up */
      geo->idbyte = 0xf8;
  } else if ( S_ISDIR(st.st_mode) ) {
    errno = EISDIR;
    return -1;
  } else {
    /* Char device, pipe or something... geometry needs to be explicit */
    errno = ESPIPE;
    return -1;
  }

  return 0;
}

#ifdef STANDALONE

const char *program;

int main(int argc, char *argv[])
{
  int fd = open(argv[1], O_RDONLY);
  struct image_geometry geo;

  program = argv[0];

  if ( fd < 0 || get_geometry(fd, &geo) ) {
    perror(argv[1]);
    exit(1);
  }

  printf("c = %lu, h = %lu, s = %lu, id = 0x%02x\n",
	 geo.c, geo.h, geo.s, geo.idbyte);

  printf("size     = %llu\n", geo.size);
  printf("secshift = %d\n", geo.secshift);
  printf("offset   = %llu\n", geo.offset);

  return 0;
}

#endif