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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "bblk_einfo.h"
#include "boot_utils.h"
bblk_hash_t bblk_no_hash = {BBLK_NO_HASH, 0, "(no hash)", NULL};
bblk_hash_t bblk_md5_hash = {BBLK_HASH_MD5, 0x10, "MD5", md5_calc};
bblk_hash_t *bblk_hash_list[BBLK_HASH_TOT] = {
&bblk_no_hash,
&bblk_md5_hash
};
/*
* einfo_compare_dotted_version()
* Compares two strings with an arbitrary long number of dot-separated numbers.
* Returns: 0 - if the version numbers are equal
* 1 - if str1 version number is more recent than str2
* 2 - if str2 version number is more recent than str1
* -1 - if an error occurred
*
* Comparison is done field by field, by retrieving an unsigned integer value,
* (missing fields are assumed as 0, but explict zeroes take precedence) so:
* 4.1.2.11 > 4.1.2.2 > 4.1.2.0 > 4.1.2
*
* where ">" means "more recent than".
*/
static int
einfo_compare_dotted_version(const char *str1, const char *str2)
{
int retval = 0;
char *verstr1, *verstr2, *freeptr1, *freeptr2;
char *parsep1, *parsep2;
unsigned int val_str1, val_str2;
freeptr1 = verstr1 = strdup(str1);
freeptr2 = verstr2 = strdup(str2);
if (verstr1 == NULL || verstr2 == NULL) {
retval = -1;
goto out;
}
while (verstr1 != NULL && verstr2 != NULL) {
parsep1 = strsep(&verstr1, ".");
parsep2 = strsep(&verstr2, ".");
val_str1 = atoi(parsep1);
val_str2 = atoi(parsep2);
if (val_str1 > val_str2) {
retval = 1;
goto out;
}
if (val_str2 > val_str1) {
retval = 2;
goto out;
}
}
/* Common portion of the version string is equal. */
if (verstr1 == NULL && verstr2 != NULL)
retval = 2;
if (verstr2 == NULL && verstr1 != NULL)
retval = 1;
out:
free(freeptr1);
free(freeptr2);
return (retval);
}
/*
* einfo_compare_timestamps()
* Currently, timestamp is in %Y%m%dT%H%M%SZ format in UTC, which means that
* we can simply do a lexicographic comparison to know which one is the most
* recent.
*
* Returns: 0 - if timestamps coincide
* 1 - if the timestamp in str1 is more recent
* 2 - if the timestamp in str2 is more recent
*/
static int
einfo_compare_timestamps(const char *str1, const char *str2)
{
int retval;
retval = strcmp(str1, str2);
if (retval > 0)
retval = 1;
if (retval < 0)
retval = 2;
return (retval);
}
/*
* einfo_compare_version()
* Given two extended versions, compare the two and returns which one is more
* "recent". Comparison is based on dotted version number fields and a
* timestamp.
*
* Returns: -1 - on error
* 0 - if the two versions coincide
* 1 - if the version in str1 is more recent
* 2 - if the version in str2 is more recent
*
* The version string generally uses following form:
* self_release,build_release:timestamp
* The release numbers are compared as dotted versions.
*
* While comparing, if the self releases are identical but the build
* release is missing, this version string is considered older.
*
* If the release strings are identical, and one of the timestamps is missing,
* we return an error. Otherwise, return the result from comparing the
* timestamps.
*/
static int
einfo_compare_version(const char *str1, const char *str2)
{
int retval = 0;
char *verstr1, *verstr2, *freeptr1, *freeptr2;
char *parsep1, *parsep2;
char *timep1, *timep2;
freeptr1 = verstr1 = strdup(str1);
freeptr2 = verstr2 = strdup(str2);
if (verstr1 == NULL || verstr2 == NULL) {
retval = -1;
goto out;
}
/* Extract the time part from the version string. */
timep1 = verstr1;
timep2 = verstr2;
parsep1 = strsep(&timep1, ":");
parsep2 = strsep(&timep2, ":");
while (parsep1 != NULL && parsep2 != NULL) {
parsep1 = strsep(&verstr1, ",-");
parsep2 = strsep(&verstr2, ",-");
/* If both are NULL, compare timestamps */
if (parsep1 == NULL && parsep2 == NULL)
break;
if (parsep1 == NULL) {
retval = 2;
goto out;
}
if (parsep2 == NULL) {
retval = 1;
goto out;
}
retval = einfo_compare_dotted_version(parsep1, parsep2);
if (retval == 0)
continue;
else
goto out;
}
/* The dotted versions are identical, check timestamps. */
if (timep1 == NULL || timep2 == NULL) {
retval = -1;
goto out;
}
retval = einfo_compare_timestamps(timep1, timep2);
out:
free(freeptr1);
free(freeptr2);
return (retval);
}
/*
* print_einfo()
*
* Print the extended information contained into the pointed structure.
* 'bufsize' specifies the real size of the structure, since str_off and
* hash_off need to point somewhere past the header.
*/
void
print_einfo(uint8_t flags, bblk_einfo_t *einfo, unsigned long bufsize)
{
int i = 0;
char *version;
boolean_t has_hash = B_FALSE;
unsigned char *hash = NULL;
if (einfo->str_off + einfo->str_size > bufsize) {
(void) fprintf(stdout, gettext("String offset %d is beyond the "
"buffer size\n"), einfo->str_off);
return;
}
version = (char *)einfo + einfo->str_off;
if (einfo->hash_type != BBLK_NO_HASH &&
einfo->hash_type < BBLK_HASH_TOT) {
if (einfo->hash_off + einfo->hash_size > bufsize) {
(void) fprintf(stdout, gettext("Warning: hashing "
"present but hash offset %d is beyond the buffer "
"size\n"), einfo->hash_off);
has_hash = B_FALSE;
} else {
hash = (unsigned char *)einfo + einfo->hash_off;
has_hash = B_TRUE;
}
}
if (flags & EINFO_PRINT_HEADER) {
(void) fprintf(stdout, "Boot Block Extended Info Header:\n");
(void) fprintf(stdout, "\tmagic: ");
for (i = 0; i < EINFO_MAGIC_SIZE; i++)
(void) fprintf(stdout, "%c", einfo->magic[i]);
(void) fprintf(stdout, "\n");
(void) fprintf(stdout, "\tversion: %d\n", einfo->version);
(void) fprintf(stdout, "\tflags: %x\n", einfo->flags);
(void) fprintf(stdout, "\textended version string offset: %d\n",
einfo->str_off);
(void) fprintf(stdout, "\textended version string size: %d\n",
einfo->str_size);
(void) fprintf(stdout, "\thashing type: %d (%s)\n",
einfo->hash_type, has_hash ?
bblk_hash_list[einfo->hash_type]->name : "nil");
(void) fprintf(stdout, "\thash offset: %d\n", einfo->hash_off);
(void) fprintf(stdout, "\thash size: %d\n", einfo->hash_size);
}
if (flags & EINFO_EASY_PARSE) {
(void) fprintf(stdout, "%s\n", version);
} else {
(void) fprintf(stdout, "Extended version string: %s\n",
version);
if (has_hash) {
(void) fprintf(stdout, "%s hash: ",
bblk_hash_list[einfo->hash_type]->name);
} else {
(void) fprintf(stdout, "No hashing available\n");
}
}
if (has_hash) {
for (i = 0; i < einfo->hash_size; i++) {
(void) fprintf(stdout, "%02x", hash[i]);
}
(void) fprintf(stdout, "\n");
}
}
static int
compute_hash(bblk_hs_t *hs, unsigned char *dest, bblk_hash_t *hash)
{
if (hs == NULL || dest == NULL || hash == NULL)
return (-1);
hash->compute_hash(dest, hs->src_buf, hs->src_size);
return (0);
}
int
prepare_and_write_einfo(unsigned char *dest, char *infostr, bblk_hs_t *hs,
uint32_t maxsize, uint32_t *used_space)
{
uint16_t hash_size;
uint32_t hash_off;
unsigned char *data;
bblk_einfo_t *einfo = (bblk_einfo_t *)dest;
bblk_hash_t *hashinfo = bblk_hash_list[BBLK_DEFAULT_HASH];
/*
* 'dest' might be both containing the buffer we want to hash and
* containing our einfo structure: delay any update of it after the
* hashing has been calculated.
*/
hash_size = hashinfo->size;
hash_off = sizeof (bblk_einfo_t);
if (hash_off + hash_size > maxsize) {
(void) fprintf(stderr, gettext("Unable to add extended info, "
"not enough space\n"));
return (-1);
}
data = dest + hash_off;
if (compute_hash(hs, data, hashinfo) < 0) {
(void) fprintf(stderr, gettext("%s hash operation failed\n"),
hashinfo->name);
einfo->hash_type = bblk_no_hash.type;
einfo->hash_size = bblk_no_hash.size;
} else {
einfo->hash_type = hashinfo->type;
einfo->hash_size = hashinfo->size;
}
(void) memcpy(einfo->magic, EINFO_MAGIC, EINFO_MAGIC_SIZE);
einfo->version = BBLK_EINFO_VERSION;
einfo->flags = 0;
einfo->hash_off = hash_off;
einfo->hash_size = hash_size;
einfo->str_off = einfo->hash_off + einfo->hash_size + 1;
if (infostr == NULL) {
(void) fprintf(stderr, gettext("Unable to add extended info, "
"string is empty\n"));
return (-1);
}
einfo->str_size = strlen(infostr);
if (einfo->str_off + einfo->str_size > maxsize) {
(void) fprintf(stderr, gettext("Unable to add extended info, "
"not enough space\n"));
return (-1);
}
data = dest + einfo->str_off;
(void) memcpy(data, infostr, einfo->str_size);
*used_space = einfo->str_off + einfo->str_size;
return (0);
}
/*
* einfo_should_update()
* Given information on the boot block currently on disk (disk_einfo) and
* information on the supplied boot block (hs for hashing, verstr as the
* associated version string) decide if an update of the on-disk boot block
* is necessary or not.
*/
boolean_t
einfo_should_update(bblk_einfo_t *disk_einfo, bblk_hs_t *hs, char *verstr)
{
bblk_hash_t *hashing;
unsigned char *disk_hash;
unsigned char *local_hash;
char *disk_version;
int retval;
if (disk_einfo == NULL)
return (B_TRUE);
if (memcmp(disk_einfo->magic, EINFO_MAGIC, EINFO_MAGIC_SIZE) != 0)
return (B_TRUE);
if (disk_einfo->version < BBLK_EINFO_VERSION)
return (B_TRUE);
disk_version = einfo_get_string(disk_einfo);
retval = einfo_compare_version(verstr, disk_version);
/*
* If something goes wrong or if the on-disk version is more recent
* do not update the bootblock.
*/
if (retval == -1 || retval == 2)
return (B_FALSE);
/*
* If we got here it means that the two version strings are either
* equal or the new bootblk binary is more recent. In order to save
* some needless writes let's use the hash to determine if an update
* is really necessary.
*/
if (disk_einfo->hash_type == bblk_no_hash.type)
return (B_TRUE);
if (disk_einfo->hash_type >= BBLK_HASH_TOT)
return (B_TRUE);
hashing = bblk_hash_list[disk_einfo->hash_type];
local_hash = malloc(hashing->size);
if (local_hash == NULL)
return (B_TRUE);
/*
* Failure in computing the hash may mean something wrong
* with the boot block file. Better be conservative here.
*/
if (compute_hash(hs, local_hash, hashing) < 0) {
free(local_hash);
return (B_FALSE);
}
disk_hash = (unsigned char *)einfo_get_hash(disk_einfo);
if (memcmp(local_hash, disk_hash, disk_einfo->hash_size) == 0) {
free(local_hash);
return (B_FALSE);
}
free(local_hash);
return (B_TRUE);
}
char *
einfo_get_string(bblk_einfo_t *einfo)
{
if (einfo == NULL)
return (NULL);
return ((char *)einfo + einfo->str_off);
}
char *
einfo_get_hash(bblk_einfo_t *einfo)
{
if (einfo == NULL)
return (NULL);
return ((char *)einfo + einfo->hash_off);
}
|