-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraspberry-unsplit.c
More file actions
294 lines (272 loc) · 10.1 KB
/
Copy pathraspberry-unsplit.c
File metadata and controls
294 lines (272 loc) · 10.1 KB
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
/*
MIT License
Copyright (c) 2019 Thomas Lovell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Whew. Do I really need that? */
#define VERS "1.01"
#define DEBUG 0 /* '1' produces debug info; '0' does not */
#define _LARGEFILE64_SOURCE
#define S 512 /* sector size */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
void hexDump(char *desc, void *addr, int len);
int main(int argc, char **argv) {
int ifd, ofd;
char bfn[S], ifn[S+8], ofn[S+5];
int ret;
ssize_t sz=0;
int errsv;
off64_t off64t, of, sectors;
unsigned short int sig=43605; /* this is 0x55aa */
unsigned int p1_lba;
unsigned int p1_num;
unsigned int p2_lba;
unsigned int p2_num;
union rec_tag {
struct mbr_tag {
char zeros[440];
int diskid;
char diskid2[2];
struct part_tag {
unsigned char status[1];
unsigned char chs_first[3];
unsigned char type[1];
unsigned char chs_last[3];
unsigned char lba[4];
unsigned char num[4];
} p1, p2, p3, p4;
unsigned char disksig[2];
} mbr;
char buf[S];
} rec;
// -------------------------------------- //
// Use either this internal base filename //
// or one from the command line. //
// -------------------------------------- //
if (argv[1] == NULL)
strncpy(bfn, "/home/pi/Downloads/raspios/2025-12-04-raspios-trixie-arm64.img", sizeof(bfn));
else
strncpy(bfn, argv[1], sizeof(bfn)-1);
printf("basename set to %s\n", bfn);
// --------------------------------- //
// append '.unsplit' to the basename //
// --------------------------------- //
strcpy(ofn, bfn);
strcat(ofn, ".unsplit");
printf("output filename set to %s\n", ofn);
// -------------------- //
// then open the output //
// -------------------- //
if ((ofd = open(ofn, O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
errsv = errno;
printf("The output file '%s' could not be opened/created.\n", ofn);
printf(" errno is '%i - %s'\n", errsv, strerror(errsv));
exit(4);
}
// ----------------------------- //
// append '.mbr' to the basename //
// ----------------------------- //
strcpy(ifn, bfn);
strcat(ifn, ".mbr");
printf("input filename set to %s\n", ifn);
// --------------------- //
// then open it as input //
// --------------------- //
if ((ifd = open(ifn, O_RDONLY | O_LARGEFILE)) == -1) {
errsv = errno;
printf("The input file '%s' could not be opened.\n", ifn);
printf(" errno is '%i - %s'\n", errsv, strerror(errsv));
exit(4);
}
// ------------------- //
// now read in the mbr //
// ------------------- //
if ((sz = read(ifd, (void *) rec.buf, S)) == S) {
if (DEBUG) {
hexDump("diskid ", &rec.mbr.diskid, 4);
hexDump("disksig ", &rec.mbr.disksig, 2);
hexDump("0x55aa ", &sig, 2);
}
if (memcmp(&rec.mbr.disksig, &sig, 2) != 0) {
printf("Input mbr file disk signature, %x%x, not 0x55aa.\n", rec.mbr.disksig[0], rec.mbr.disksig[1]);
exit(4);
}
} else {
printf("size read, %li, not %i\n", sz, S);
exit(4);
}
// --------------------------------------------------------------- //
// save the start and number of sectors of the original partitions //
// --------------------------------------------------------------- //
memcpy(&p1_lba, &rec.mbr.p1.lba, 4);
memcpy(&p1_num, &rec.mbr.p1.num, 4);
memcpy(&p2_lba, &rec.mbr.p2.lba, 4);
memcpy(&p2_num, &rec.mbr.p2.num, 4);
// ------------- //
// write the mbr //
// ------------- //
write(ofd, rec.buf, S);
// ------------------- //
// and close the input //
// ------------------- //
close(ifd);
// -------------------------------------------------------------------- //
// now position the output file to where the 1st partition should start //
// -------------------------------------------------------------------- //
of = p1_lba*S;
off64t = lseek64(ofd, of, SEEK_SET);
// ------------------------------ //
// how many sectors we're copying //
// ------------------------------ //
sectors = p1_num;
// ------------------------------------------ //
// append '.boot' to the basename of the input //
// ------------------------------------------ //
strcpy(ifn, bfn);
strcat(ifn, ".boot");
printf("input filename set to %s\n", ifn);
// ------------------------------- //
// open the input file, i.e. .boot //
// ------------------------------- //
if ((ifd = open(ifn, O_RDONLY | O_LARGEFILE)) == -1) {
errsv = errno;
printf("The input file '%s' could not be opened.\n", ifn);
printf(" errno is '%i - %s'\n", errsv, strerror(errsv));
exit(4);
}
// ------------------------- //
// and send it to the output //
// ------------------------- //
while (sectors--) {
if (!(sz = (read(ifd, rec.buf, S)) == S)) {
printf ("size read, %li, not %i\n", sz, S);
exit(4);
}
write(ofd, rec.buf, S);
}
// --------------------------------------------------------------------- //
// finally, close the input in anticipation of writing the 2nd partition //
// --------------------------------------------------------------------- //
close(ifd);
// -------------------------------------------------------------------- //
// now position the output file to where the 2nd partition should start //
// -------------------------------------------------------------------- //
of = p2_lba*S;
off64t = lseek64(ofd, of, SEEK_SET);
// ------------------------------ //
// how many sectors we're copying //
// ------------------------------ //
sectors = p2_num;
// ------------------------------------------ //
// append '.root' to the basename of the input //
// ------------------------------------------ //
strcpy(ifn, bfn);
strcat(ifn, ".root");
printf("input filename set to %s\n", ifn);
// ------------------------------- //
// open the input file, i.e. .root //
// ------------------------------- //
if ((ifd = open(ifn, O_RDONLY | O_LARGEFILE)) == -1) {
errsv = errno;
printf("The input file '%s' could not be opened.\n", ifn);
printf(" errno is '%i - %s'\n", errsv, strerror(errsv));
exit(4);
}
// ------------------------- //
// and send it to the output //
// ------------------------- //
while (sectors--) {
if (!(sz = (read(ifd, rec.buf, S)) == S)) {
printf ("size read, %li, not %i\n", sz, S);
exit(4);
}
write(ofd, rec.buf, S);
}
// --------------- //
// close the input //
// --------------- //
close(ofd);
// ---------------- //
// close the output //
// ---------------- //
close(ifd);
// -------------- //
// and we're done //
// -------------- //
exit(0);
}
void hexDump(char *desc, void *addr, int len) {
/*
MIT License
Copyright (c) 2019 Thomas Lovell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
// Output description if given.
if (desc != NULL)
printf ("%s:\n", desc);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with lne offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf (" %s\n", buff);
// Output the offset.
printf (" %04x ", i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s\n", buff);
}