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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [disk/] [tools/] [wrpart/] [wrpart.c] - Blame information for rev 294

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

Line No. Rev Author Line
1 265 hellwig
/*
2 266 hellwig
 * wrpart.c -- write a binary image to a partition on a disk
3 265 hellwig
 */
4
 
5
 
6 266 hellwig
#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 int type;
19
  unsigned int start;
20
  unsigned int 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 int getNumber(unsigned char *p) {
46
  return (unsigned int) *(p + 0) << 24 |
47
         (unsigned int) *(p + 1) << 16 |
48
         (unsigned int) *(p + 2) <<  8 |
49
         (unsigned int) *(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 265 hellwig
int main(int argc, char *argv[]) {
70 266 hellwig
  char *diskName;
71
  char *partNmbr;
72
  char *partName;
73
  FILE *disk;
74
  unsigned int diskSize;
75
  char *endp;
76
  int partno;
77
  unsigned int partStart;
78
  unsigned int partSize;
79
  FILE *image;
80
  unsigned int imageSize;
81
  unsigned int i;
82
  unsigned char sectBuf[SECTOR_SIZE];
83
  int n;
84
 
85
  /* check command line arguments */
86
  if (argc != 4) {
87
    printf("Usage: %s <disk image> ", argv[0]);
88
    printf("<partition number> <partition image>\n");
89
    exit(1);
90
  }
91
  diskName = argv[1];
92
  partNmbr = argv[2];
93
  partName = argv[3];
94
  /* read partition table record */
95
  disk = fopen(diskName, "r+b");
96
  if (disk == NULL) {
97
    error("cannot open disk image '%s'", diskName);
98
  }
99
  fseek(disk, 0, SEEK_END);
100
  diskSize = ftell(disk) / SECTOR_SIZE;
101
  printf("disk '%s' has %u (0x%X) sectors\n",
102
         diskName, diskSize, diskSize);
103
  fseek(disk, 1 * SECTOR_SIZE, SEEK_SET);
104
  if (fread(ptr, 1, SECTOR_SIZE, disk) != SECTOR_SIZE) {
105
    error("cannot read partition table from disk image '%s'", diskName);
106
  }
107
  convertPartitionTable(ptr, NPE);
108 269 hellwig
  /* get partition number, determine start and size of partition */
109 266 hellwig
  partno = strtol(partNmbr, &endp, 10);
110
  if (*endp != '\0') {
111
    error("cannot read partition number");
112
  }
113
  if (partno < 0 || partno > 15) {
114
    error("illegal partition number %d", partno);
115
  }
116
  if ((ptr[partno].type & 0x7FFFFFFF) == 0) {
117
    error("partition %d is not allocated in partition table", partno);
118
  }
119
  partStart = ptr[partno].start;
120
  partSize = ptr[partno].size;
121
  printf("partition %d: start sector %u (0x%X), size is %u (0x%X) sectors\n",
122
         partno, partStart, partStart, partSize, partSize);
123
  if (partStart >= diskSize || partStart + partSize > diskSize) {
124
    error("partition %d is larger than the disk", partno);
125
  }
126
  fseek(disk, partStart * SECTOR_SIZE, SEEK_SET);
127 269 hellwig
  /* open partition image, check size (rounded up to whole sectors) */
128 266 hellwig
  image = fopen(partName, "rb");
129
  if (image == NULL) {
130
    error("cannot open partition image '%s'", partName);
131
  }
132
  fseek(image, 0, SEEK_END);
133
  imageSize = (ftell(image) + SECTOR_SIZE - 1) / SECTOR_SIZE;
134
  printf("partition image '%s' occupies %d (0x%X) sectors\n",
135
         partName, imageSize, imageSize);
136 269 hellwig
  if (imageSize > partSize) {
137 266 hellwig
    error("partition image (%d sectors) too big for partition (%d sectors)",
138
          imageSize, partSize);
139
  }
140
  fseek(image, 0, SEEK_SET);
141
  /* copy partition image to partition on disk */
142
  for (i = 0; i < imageSize; i++) {
143
    n = fread(sectBuf, 1, SECTOR_SIZE, image);
144
    if (n != SECTOR_SIZE) {
145
      if (i != imageSize - 1) {
146
        error("cannot read partition image '%s'", partName);
147
      } else {
148
        while (n < SECTOR_SIZE) {
149
          sectBuf[n++] = 0;
150
        }
151
      }
152
    }
153
    n = fwrite(sectBuf, 1, SECTOR_SIZE, disk);
154
    if (n != SECTOR_SIZE) {
155
      error("cannot write disk image '%s'", diskName);
156
    }
157
  }
158
  printf("partition image '%s' (%d sectors) copied to partition %d\n",
159
         partName, imageSize, partno);
160
  fclose(image);
161
  fclose(disk);
162 265 hellwig
  return 0;
163
}

powered by: WebSVN 2.1.0

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