OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [disk/] [tools/] [shpart/] [shpart.c] - Blame information for rev 17

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*
2
 * shpart.c -- show partitions on a disk
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
#define SECTOR_SIZE     512
13
#define NPE             (SECTOR_SIZE / sizeof(PartEntry))
14
#define DESCR_SIZE      20
15
 
16
 
17
typedef struct {
18
  unsigned long type;
19
  unsigned long start;
20
  unsigned long size;
21
  char descr[DESCR_SIZE];
22
} PartEntry;
23
 
24
PartEntry ptr[NPE];
25
 
26
 
27
/**************************************************************/
28
 
29
 
30
void error(char *fmt, ...) {
31
  va_list ap;
32
 
33
  va_start(ap, fmt);
34
  printf("Error: ");
35
  vprintf(fmt, ap);
36
  printf("\n");
37
  va_end(ap);
38
  exit(1);
39
}
40
 
41
 
42
/**************************************************************/
43
 
44
 
45
unsigned long getNumber(unsigned char *p) {
46
  return (unsigned long) *(p + 0) << 24 |
47
         (unsigned long) *(p + 1) << 16 |
48
         (unsigned long) *(p + 2) <<  8 |
49
         (unsigned long) *(p + 3) <<  0;
50
}
51
 
52
 
53
void convertPartitionTable(PartEntry *e, int n) {
54
  int i;
55
  unsigned char *p;
56
 
57
  for (i = 0; i < n; i++) {
58
    p = (unsigned char *) &e[i];
59
    e[i].type = getNumber(p + 0);
60
    e[i].start = getNumber(p + 4);
61
    e[i].size = getNumber(p + 8);
62
  }
63
}
64
 
65
 
66
/**************************************************************/
67
 
68
 
69
int main(int argc, char *argv[]) {
70
  char *diskName;
71
  FILE *disk;
72
  unsigned long diskSize;
73
  unsigned long numSectors;
74
  unsigned long partLast;
75
  int i, j;
76
  char c;
77
 
78
  /* check command line arguments */
79
  if (argc != 2) {
80
    printf("Usage: %s <disk image file>\n", argv[0]);
81
    exit(1);
82
  }
83
  diskName = argv[1];
84
  /* determine disk size */
85
  disk = fopen(diskName, "rb");
86
  if (disk == NULL) {
87
    error("cannot open disk image '%s'", diskName);
88
  }
89
  fseek(disk, 0, SEEK_END);
90
  diskSize = ftell(disk);
91
  numSectors = diskSize / SECTOR_SIZE;
92
  fclose(disk);
93
  printf("Disk '%s' has %lu (0x%lX) sectors.\n",
94
         diskName, numSectors, numSectors);
95
  if (numSectors < 32) {
96
    error("disk is too small");
97
  }
98
  if (diskSize % SECTOR_SIZE != 0) {
99
    printf("Warning: disk size is not a multiple of sector size!\n");
100
  }
101
  /* read partition table record */
102
  disk = fopen(diskName, "rb");
103
  if (disk == NULL) {
104
    error("cannot open disk image '%s'", diskName);
105
  }
106
  fseek(disk, 1 * SECTOR_SIZE, SEEK_SET);
107
  if (fread(ptr, 1, SECTOR_SIZE, disk) != SECTOR_SIZE) {
108
    error("cannot read partition table from disk image '%s'", diskName);
109
  }
110
  fclose(disk);
111
  convertPartitionTable(ptr, NPE);
112
  /* show partition table */
113
  printf("Partitions:\n");
114
  printf(" # b type       start      last       size       description\n");
115
  for (i = 0; i < NPE; i++) {
116
    if (ptr[i].type != 0) {
117
      partLast = ptr[i].start + ptr[i].size - 1;
118
    } else {
119
      partLast = 0;
120
    }
121
    printf("%2d %s 0x%08lX 0x%08lX 0x%08lX 0x%08lX ",
122
           i,
123
           ptr[i].type & 0x80000000 ? "*" : " ",
124
           ptr[i].type & 0x7FFFFFFF,
125
           ptr[i].start,
126
           partLast,
127
           ptr[i].size);
128
    for (j = 0; j < DESCR_SIZE; j++) {
129
      c = ptr[i].descr[j];
130
      if (c == '\0') {
131
        break;
132
      }
133
      if (c >= 0x20 && c < 0x7F) {
134
        printf("%c", c);
135
      } else {
136
        printf(".");
137
      }
138
    }
139
    printf("\n");
140
  }
141
  /* done */
142
  return 0;
143
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.