summaryrefslogtreecommitdiffstats
path: root/getgeo.c
blob: 1de94637fc4e33386f5e292adeff589a0405b956 (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
#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,  160<<10, 0, 9 },
    { 40, 1,  9,  180<<10, 0, 9 },
    { 40, 2,  8,  320<<10, 0, 9 },
    { 40, 2,  9,  360<<10, 0, 9 },
    { 80, 2,  9,  720<<10, 0, 9 },
    { 80, 2, 15, 1200<<10, 0, 9 },
    { 80, 2, 18, 1440<<10, 0, 9 },
    { 80, 2, 36, 2880<<10, 0, 9 },
    { 0, 0, 0, 0, 0, 0 }
  };

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

  if ( S_ISBLK(st.st_mode) ) {
#if 1 /*def 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, 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->offset = (uint64_t)hdgeo.start << geo->secshift;
    } else if ( !ioctl(fd, FDGETPRM, &fdgeo) ) {
      geo->c = fdgeo.track;	/* IS THIS CORRECT?!? */
      geo->h = fdgeo.head;
      geo->s = fdgeo.sect;
    }
  } 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;
	}
      }

      /* DO SOMETHING! */
  } 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\n",
	 geo.c, geo.h, geo.s);

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

  return 0;
}

#endif