forked from makedumpfile/makedumpfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sadump_info.c
2550 lines (2118 loc) · 66.4 KB
/
sadump_info.c
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sadump_info.c
*
* Created by: HATAYAMA, Daisuke <[email protected]>
*
* Copyright (C) 2011 FUJITSU LIMITED
* Copyright (C) 2011 NEC Corporation
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#if defined(__x86__) || defined(__x86_64__)
#include "makedumpfile.h"
#include "elf_info.h"
#include "print_info.h"
#include "sadump_mod.h"
#include <arpa/inet.h> /* htonl, htons */
#define SADUMP_EFI_GUID_TEXT_REPR_LEN 36
#ifdef __x86__
#define KEXEC_NOTE_HEAD_BYTES roundup(sizeof(Elf32_Nhdr), 4)
#endif
#ifdef __x86_64__
#define MEGABYTES(x) ((x) * (1048576))
#define KEXEC_NOTE_HEAD_BYTES roundup(sizeof(Elf64_Nhdr), 4)
#endif
#define KEXEC_CORE_NOTE_DESC_BYTES roundup(sizeof(struct elf_prstatus), 4)
#define KEXEC_NOTE_BYTES ((KEXEC_NOTE_HEAD_BYTES * 2) + \
roundup(KEXEC_CORE_NOTE_NAME_BYTES, 4) + \
KEXEC_CORE_NOTE_DESC_BYTES )
#define for_each_online_cpu(cpu) \
for (cpu = 0; cpu < max_mask_cpu(); ++cpu) \
if (is_online_cpu(cpu))
enum {
BITPERWORD = BITPERBYTE * sizeof(unsigned long)
};
struct sadump_diskset_info {
char *name_memory;
int fd_memory;
struct sadump_part_header *sph_memory;
unsigned long data_offset;
};
struct sadump_info {
struct sadump_part_header *sph_memory;
struct sadump_header *sh_memory;
struct sadump_disk_set_header *sdh_memory;
struct sadump_media_header *smh_memory;
struct sadump_diskset_info *diskset_info;
int num_disks;
unsigned long sub_hdr_offset;
uint32_t smram_cpu_state_size;
unsigned long data_offset;
unsigned long long *block_table;
unsigned long *__per_cpu_offset;
unsigned long __per_cpu_load;
FILE *file_elf_note;
char *cpu_online_mask_buf;
size_t cpumask_size;
/* Backup Region, First 640K of System RAM. */
#define KEXEC_BACKUP_SRC_END 0x0009ffff
unsigned long long backup_src_start;
unsigned long backup_src_size;
unsigned long long backup_offset;
int kdump_backed_up;
mdf_pfn_t max_mapnr;
struct dump_bitmap *ram_bitmap;
};
static char *guid_to_str(efi_guid_t *guid, char *buf, size_t buflen);
static struct tm *efi_time_t_to_tm(const efi_time_t *e);
static int verify_magic_number(uint32_t magicnum[DUMP_PART_HEADER_MAGICNUM_SIZE]);
static int read_device(void *buf, size_t bytes, ulong *offset);
static int read_device_diskset(struct sadump_diskset_info *sdi, void *buf,
size_t bytes, ulong *offset);
static int read_sadump_header(char *filename);
static int read_sadump_header_diskset(int diskid, struct sadump_diskset_info *sdi);
static unsigned long long pfn_to_block(mdf_pfn_t pfn);
static int lookup_diskset(unsigned long long whole_offset, int *diskid,
unsigned long long *disk_offset);
static int max_mask_cpu(void);
static int cpu_online_mask_init(void);
static int per_cpu_init(void);
static int get_data_from_elf_note_desc(const char *note_buf, uint32_t n_descsz,
char *name, uint32_t n_type, char **data);
static int alignfile(unsigned long *offset);
static int
write_elf_note_header(char *name, void *data, size_t descsz, uint32_t type,
unsigned long *offset, unsigned long *desc_offset);
static int is_online_cpu(int cpu);
static unsigned long legacy_per_cpu_ptr(unsigned long ptr, int cpu);
static unsigned long per_cpu_ptr(unsigned long ptr, int cpu);
static int get_prstatus_from_crash_notes(int cpu, char *prstatus_buf);
static int cpu_to_apicid(int cpu, int *apicid);
static int get_smram_cpu_state(int apicid, struct sadump_smram_cpu_state *smram);
static int copy_regs_from_prstatus(struct elf_prstatus *prstatus,
const char *prstatus_buf);
static int
copy_regs_from_smram_cpu_state(struct elf_prstatus *prstatus,
const struct sadump_smram_cpu_state *smram);
static void
debug_message_smram_cpu_state(int apicid, struct sadump_smram_cpu_state *s);
static void
debug_message_user_regs_struct(int cpu, struct elf_prstatus *prstatus);
static int get_registers(int cpu, struct elf_prstatus *prstatus);
static struct sadump_info sadump_info = {};
static struct sadump_info *si = &sadump_info;
static inline int
sadump_is_on(char *bitmap, mdf_pfn_t i)
{
return bitmap[i >> 3] & (1 << (7 - (i & 7)));
}
static inline int
sadump_is_dumpable(struct dump_bitmap *bitmap, mdf_pfn_t pfn)
{
off_t offset;
ssize_t rcode;
if (pfn == 0 || bitmap->no_block != pfn/PFN_BUFBITMAP) {
offset = bitmap->offset + BUFSIZE_BITMAP*(pfn/PFN_BUFBITMAP);
lseek(bitmap->fd, offset, SEEK_SET);
rcode = read(bitmap->fd, bitmap->buf, BUFSIZE_BITMAP);
if (rcode != BUFSIZE_BITMAP)
ERRMSG("Can't read the bitmap(%s). %s\n",
bitmap->file_name, strerror(errno));
if (pfn == 0)
bitmap->no_block = 0;
else
bitmap->no_block = pfn / PFN_BUFBITMAP;
}
return sadump_is_on(bitmap->buf, pfn % PFN_BUFBITMAP);
}
static inline int
sadump_is_ram(mdf_pfn_t pfn)
{
return sadump_is_dumpable(si->ram_bitmap, pfn);
}
int
check_and_get_sadump_header_info(char *filename)
{
int i;
if (!read_sadump_header(filename))
return FALSE;
if (info->flag_sadump_diskset && info->flag_sadump == SADUMP_DISKSET) {
si->diskset_info[0].fd_memory = info->fd_memory;
si->diskset_info[0].sph_memory = si->sph_memory;
si->diskset_info[0].data_offset = si->data_offset;
for (i = 1; i < si->num_disks; ++i) {
struct sadump_diskset_info *sdi =
&si->diskset_info[i];
if ((sdi->fd_memory =
open(sdi->name_memory, O_RDONLY)) < 0) {
ERRMSG("Can't open the dump diskset "
"memory(%s). %s\n", sdi->name_memory,
strerror(errno));
return FALSE;
}
if (!read_sadump_header_diskset(i, sdi))
return FALSE;
}
}
return TRUE;
}
static void
reverse_bit(char *buf, int len)
{
int i;
unsigned char c;
for (i = 0; i < len; i++) {
c = buf[i];
c = ((c & 0x55) << 1) | ((c & 0xaa) >> 1); /* Swap 1bit */
c = ((c & 0x33) << 2) | ((c & 0xcc) >> 2); /* Swap 2bit */
c = (c << 4) | (c >> 4); /* Swap 4bit */
buf[i] = c;
}
}
int
sadump_copy_1st_bitmap_from_memory(void)
{
struct sadump_header *sh = si->sh_memory;
char buf[si->sh_memory->block_size];
off_t offset_page;
unsigned long bitmap_offset, bitmap_len;
mdf_pfn_t pfn, pfn_bitmap1;
extern mdf_pfn_t pfn_memhole;
bitmap_offset = si->sub_hdr_offset + sh->block_size*sh->sub_hdr_size;
bitmap_len = sh->block_size * sh->bitmap_blocks;
if (lseek(info->fd_memory, bitmap_offset, SEEK_SET) < 0) {
ERRMSG("Can't seek %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
if (lseek(info->bitmap1->fd, info->bitmap1->offset, SEEK_SET) < 0) {
ERRMSG("Can't seek the bitmap(%s). %s\n",
info->bitmap1->file_name, strerror(errno));
return FALSE;
}
offset_page = 0;
while (offset_page < bitmap_len) {
if (read(info->fd_memory, buf, sizeof(buf)) != sizeof(buf)) {
ERRMSG("Can't read %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* sadump formats associate each bit in a bitmap with
* a physical page in reverse order with the
* kdump-compressed format. We need to change bit
* order to reuse bitmaps in sadump formats in the
* kdump-compressed format.
*/
reverse_bit(buf, sizeof(buf));
if (write(info->bitmap1->fd, buf, sizeof(buf)) != sizeof(buf)) {
ERRMSG("Can't write the bitmap(%s). %s\n",
info->bitmap1->file_name, strerror(errno));
return FALSE;
}
offset_page += sizeof(buf);
}
pfn_bitmap1 = 0;
for (pfn = 0; pfn < info->max_mapnr; ++pfn) {
if (sadump_is_ram(pfn))
pfn_bitmap1++;
}
pfn_memhole = info->max_mapnr - pfn_bitmap1;
/*
* kdump uses the first 640kB on the 2nd kernel. But both
* bitmaps should reflect the 1st kernel memory situation. We
* modify bitmap accordingly.
*/
if (si->kdump_backed_up) {
unsigned long long paddr;
mdf_pfn_t pfn, backup_src_pfn;
for (paddr = si->backup_src_start;
paddr < si->backup_src_start + si->backup_src_size;
paddr += info->page_size) {
pfn = paddr_to_pfn(paddr);
backup_src_pfn = paddr_to_pfn(paddr +
si->backup_offset -
si->backup_src_start);
if (is_dumpable(info->bitmap_memory, backup_src_pfn, NULL))
set_bit_on_1st_bitmap(pfn, NULL);
else
clear_bit_on_1st_bitmap(pfn, NULL);
}
}
return TRUE;
}
int
sadump_generate_vmcoreinfo_from_vmlinux(size_t *vmcoreinfo_size)
{
size_t size;
if (!info->file_vmcoreinfo)
return FALSE;
if ((SYMBOL(system_utsname) == NOT_FOUND_SYMBOL) &&
(SYMBOL(init_uts_ns) == NOT_FOUND_SYMBOL)) {
ERRMSG("Can't get the symbol of system_utsname.\n");
return FALSE;
}
if (get_mem_type() == NOT_FOUND_MEMTYPE) {
ERRMSG("Can't find the memory type.\n");
return FALSE;
}
strncpy(info->release, info->system_utsname.release,
STRLEN_OSRELEASE);
write_vmcoreinfo_data();
size = ftell(info->file_vmcoreinfo);
*vmcoreinfo_size = size;
return TRUE;
}
int
sadump_generate_elf_note_from_dumpfile(void)
{
size_t size_vmcoreinfo, size_pt_note;
int x_cpu;
unsigned long offset, offset_vmcoreinfo;
char *vmcoreinfo_buf = NULL;
int retval = FALSE;
if (!per_cpu_init())
return FALSE;
if (!(info->file_vmcoreinfo = tmpfile())) {
ERRMSG("Can't create a temporary strings(%s).\n",
FILENAME_VMCOREINFO);
return FALSE;
}
if (!sadump_generate_vmcoreinfo_from_vmlinux(&size_vmcoreinfo)) {
ERRMSG("Can't generate vmcoreinfo data.\n");
goto error;
}
if ((vmcoreinfo_buf = malloc(size_vmcoreinfo)) == NULL) {
ERRMSG("Can't allocate vmcoreinfo buffer. %s\n",
strerror(errno));
goto cleanup;
}
rewind(info->file_vmcoreinfo);
if (fread(vmcoreinfo_buf, size_vmcoreinfo, 1,
info->file_vmcoreinfo) != 1) {
ERRMSG("Can't read vmcoreinfo temporary file. %s\n",
strerror(errno));
goto cleanup;
}
if (!(si->file_elf_note = tmpfile())) {
ERRMSG("Can't create a temporary elf_note file. %s\n",
strerror(errno));
goto cleanup;
}
if (!cpu_online_mask_init())
goto cleanup;
offset = 0;
for_each_online_cpu(x_cpu) {
struct elf_prstatus prstatus;
memset(&prstatus, 0, sizeof(prstatus));
if (!get_registers(x_cpu, &prstatus))
goto cleanup;
if (!write_elf_note_header("CORE", &prstatus, sizeof(prstatus),
NT_PRSTATUS, &offset, NULL))
goto cleanup;
}
if (!write_elf_note_header("VMCOREINFO", vmcoreinfo_buf,
size_vmcoreinfo, 0, &offset,
&offset_vmcoreinfo))
goto cleanup;
size_pt_note = ftell(si->file_elf_note);
set_pt_note(0, size_pt_note);
set_vmcoreinfo(offset_vmcoreinfo, size_vmcoreinfo);
retval = TRUE;
cleanup:
free(vmcoreinfo_buf);
if (info->file_vmcoreinfo) {
fclose(info->file_vmcoreinfo);
info->file_vmcoreinfo = NULL;
}
error:
return retval;
}
static char *
guid_to_str(efi_guid_t *guid, char *buf, size_t buflen)
{
snprintf(buf, buflen,
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
htonl(guid->data1), htons(guid->data2), htons(guid->data3),
guid->data4[0], guid->data4[1], guid->data4[2],
guid->data4[3], guid->data4[4], guid->data4[5],
guid->data4[6], guid->data4[7]);
return buf;
}
static struct tm *
efi_time_t_to_tm(const efi_time_t *e)
{
static struct tm t;
time_t ti;
memset(&t, 0, sizeof(t));
t.tm_sec = e->second;
t.tm_min = e->minute;
t.tm_hour = e->hour;
t.tm_mday = e->day;
t.tm_mon = e->month - 1;
t.tm_year = e->year - 1900;
if (e->timezone != EFI_UNSPECIFIED_TIMEZONE)
t.tm_hour += e->timezone;
else
DEBUG_MSG("sadump: timezone information is missing\n");
ti = mktime(&t);
if (ti == (time_t)-1)
return &t;
return localtime_r(&ti, &t);
}
static int
verify_magic_number(uint32_t magicnum[DUMP_PART_HEADER_MAGICNUM_SIZE])
{
int i;
for (i = 1; i < DUMP_PART_HEADER_MAGICNUM_SIZE; ++i)
if (magicnum[i] != (magicnum[i - 1] + 7) * 11)
return FALSE;
return TRUE;
}
static int
read_device(void *buf, size_t bytes, ulong *offset)
{
if (lseek(info->fd_memory, *offset, SEEK_SET) < 0) {
ERRMSG("Can't seek a file(%s). %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
if (read(info->fd_memory, buf, bytes) != bytes) {
ERRMSG("Can't read a file(%s). %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
*offset += bytes;
return TRUE;
}
static int
read_device_diskset(struct sadump_diskset_info *sdi, void *buf,
size_t bytes, unsigned long *offset)
{
if (lseek(sdi->fd_memory, *offset, SEEK_SET) < 0) {
ERRMSG("Can't seek a file(%s). %s\n",
sdi->name_memory, strerror(errno));
return FALSE;
}
if (read(sdi->fd_memory, buf, bytes) != bytes) {
ERRMSG("Can't read a file(%s). %s\n",
sdi->name_memory, strerror(errno));
return FALSE;
}
*offset += bytes;
return TRUE;
}
static int
read_sadump_header(char *filename)
{
struct sadump_part_header *sph = NULL;
struct sadump_header *sh = NULL;
struct sadump_disk_set_header *sdh = NULL;
struct sadump_media_header *smh = NULL;
unsigned long offset = 0, sub_hdr_offset;
unsigned long block_size = SADUMP_DEFAULT_BLOCK_SIZE;
unsigned long bitmap_len, dumpable_bitmap_len;
enum sadump_format_type flag_sadump;
uint32_t smram_cpu_state_size = 0;
char guid[SADUMP_EFI_GUID_TEXT_REPR_LEN+1];
if ((si->sph_memory = malloc(SADUMP_DEFAULT_BLOCK_SIZE)) == NULL) {
ERRMSG("Can't allocate memory for partition header buffer: "
"%s\n", strerror(errno));
return FALSE;
}
if ((si->sh_memory = malloc(SADUMP_DEFAULT_BLOCK_SIZE)) == NULL) {
ERRMSG("Can't allocate memory for dump header buffer: "
"%s\n", strerror(errno));
return FALSE;
}
if ((si->sdh_memory = malloc(SADUMP_DEFAULT_BLOCK_SIZE)) == NULL) {
ERRMSG("Can't allocate memory for disk set header buffer: "
"%s\n", strerror(errno));
return FALSE;
}
if ((si->smh_memory = malloc(SADUMP_DEFAULT_BLOCK_SIZE)) == NULL) {
ERRMSG("Can't allocate memory for media header buffer: "
"%s\n", strerror(errno));
return FALSE;
}
sph = si->sph_memory;
sh = si->sh_memory;
sdh = si->sdh_memory;
smh = si->smh_memory;
restart:
if (!read_device(sph, block_size, &offset))
return ERROR;
if (sph->signature1 == SADUMP_SIGNATURE1 &&
sph->signature2 == SADUMP_SIGNATURE2) {
if (sph->set_disk_set == 0) {
flag_sadump = SADUMP_SINGLE_PARTITION;
DEBUG_MSG("sadump: read dump device as single partition\n");
} else {
flag_sadump = SADUMP_DISKSET;
DEBUG_MSG("sadump: read dump device as diskset\n");
}
} else {
offset = 0;
if (!read_device(smh, block_size, &offset))
return ERROR;
if (!read_device(sph, block_size, &offset))
return ERROR;
if (sph->signature1 != SADUMP_SIGNATURE1 ||
sph->signature2 != SADUMP_SIGNATURE2) {
DEBUG_MSG("sadump: does not have partition header\n");
flag_sadump = SADUMP_UNKNOWN;
DEBUG_MSG("sadump: read dump device as unknown format\n");
goto out;
}
flag_sadump = SADUMP_MEDIA_BACKUP;
DEBUG_MSG("sadump: read dump device as media backup format\n");
}
if (!verify_magic_number(sph->magicnum)) {
DEBUG_MSG("sadump: invalid magic number\n");
return FALSE;
}
if (flag_sadump == SADUMP_DISKSET) {
uint32_t header_blocks;
size_t header_size;
if (sph->set_disk_set != 1) {
DEBUG_MSG("sadump: id of this disk is %d\n",
sph->set_disk_set);
return FALSE;
}
if (!read_device(&header_blocks, sizeof(uint32_t),
&offset))
return FALSE;
offset -= sizeof(uint32_t);
header_size = header_blocks * block_size;
if (header_size > block_size) {
sdh = realloc(sdh, header_size);
if (!sdh) {
ERRMSG("Can't allocate memory for disk "
"set memory\n");
return FALSE;
}
}
if (!read_device(sdh, header_size, &offset))
return ERROR;
DEBUG_MSG("sadump: the diskset consists of %u disks\n",
sdh->disk_num);
}
if (!read_device(sh, block_size, &offset))
return FALSE;
sub_hdr_offset = offset;
if (strncmp(sh->signature, SADUMP_SIGNATURE, 8) != 0) {
DEBUG_MSG("sadump: does not have dump header\n");
return FALSE;
}
if (flag_sadump == SADUMP_MEDIA_BACKUP) {
if (memcmp(&sph->sadump_id, &smh->sadump_id,
sizeof(efi_guid_t)) != 0) {
DEBUG_MSG("sadump: system ID mismatch\n");
DEBUG_MSG(" partition header: %s\n",
guid_to_str(&sph->sadump_id, guid,
sizeof(guid)));
DEBUG_MSG(" media header: %s\n",
guid_to_str(&smh->sadump_id, guid,
sizeof(guid)));
return FALSE;
}
if (memcmp(&sph->disk_set_id, &smh->disk_set_id,
sizeof(efi_guid_t)) != 0) {
DEBUG_MSG("sadump: disk set ID mismatch\n");
DEBUG_MSG(" partition header: %s\n",
guid_to_str(&sph->disk_set_id, guid,
sizeof(guid)));
DEBUG_MSG(" media header: %s\n",
guid_to_str(&smh->disk_set_id, guid,
sizeof(guid)));
return FALSE;
}
if (memcmp(&sph->time_stamp, &smh->time_stamp,
sizeof(efi_time_t)) != 0) {
DEBUG_MSG("sadump: time stamp mismatch\n");
DEBUG_MSG(" partition header: %s",
asctime(efi_time_t_to_tm(&sph->time_stamp)));
DEBUG_MSG(" media header: %s",
asctime(efi_time_t_to_tm(&smh->time_stamp)));
}
if (smh->sequential_num != 1) {
DEBUG_MSG("sadump: first media file has sequential "
"number %d\n", smh->sequential_num);
return FALSE;
}
}
if (sh->block_size != block_size) {
block_size = sh->block_size;
offset = 0;
goto restart;
}
if (sh->sub_hdr_size > 0) {
if (!read_device(&smram_cpu_state_size, sizeof(uint32_t),
&offset)) {
DEBUG_MSG("sadump: cannot read SMRAM CPU STATE size\n");
return FALSE;
}
smram_cpu_state_size /= sh->nr_cpus;
offset -= sizeof(uint32_t);
offset += sh->sub_hdr_size * block_size;
}
switch (sh->header_version) {
case 0:
si->max_mapnr = (mdf_pfn_t)(uint64_t)sh->max_mapnr;
break;
default:
ERRMSG("sadump: unsupported header version: %u\n"
"sadump: assuming header version: 1\n",
sh->header_version);
case 1:
si->max_mapnr = (mdf_pfn_t)sh->max_mapnr_64;
break;
}
if (!sh->bitmap_blocks) {
DEBUG_MSG("sadump: bitmap_blocks is zero\n");
return FALSE;
}
if (!sh->dumpable_bitmap_blocks) {
DEBUG_MSG("sadump: dumpable_bitmap_blocks is zero\n");
return FALSE;
}
bitmap_len = block_size * sh->bitmap_blocks;
dumpable_bitmap_len = block_size * sh->dumpable_bitmap_blocks;
si->sub_hdr_offset = sub_hdr_offset;
si->smram_cpu_state_size = smram_cpu_state_size;
si->data_offset = offset + bitmap_len + dumpable_bitmap_len;
out:
switch (flag_sadump) {
case SADUMP_SINGLE_PARTITION:
DEBUG_MSG("sadump: single partition configuration\n");
break;
case SADUMP_DISKSET:
DEBUG_MSG("sadump: diskset configuration with %d disks\n",
sdh->disk_num);
break;
case SADUMP_MEDIA_BACKUP:
DEBUG_MSG("sadump: media backup file\n");
break;
case SADUMP_UNKNOWN:
DEBUG_MSG("sadump: unknown format\n");
break;
}
info->flag_sadump = flag_sadump;
return TRUE;
}
static int
read_sadump_header_diskset(int diskid, struct sadump_diskset_info *sdi)
{
struct sadump_part_header *sph = NULL;
unsigned long offset = 0;
char guid[SADUMP_EFI_GUID_TEXT_REPR_LEN+1];
if ((sph = malloc(si->sh_memory->block_size)) == NULL) {
ERRMSG("Can't allocate memory for partition header buffer. "
"%s\n", strerror(errno));
goto error;
}
if (!read_device_diskset(sdi, sph, si->sh_memory->block_size,
&offset))
goto error;
if (sph->signature1 != SADUMP_SIGNATURE1 ||
sph->signature2 != SADUMP_SIGNATURE2) {
DEBUG_MSG("sadump: does not have partition header\n");
goto error;
}
if (memcmp(&si->sph_memory->sadump_id, &sph->sadump_id,
sizeof(efi_guid_t)) != 0) {
DEBUG_MSG("sadump: system ID mismatch\n");
DEBUG_MSG(" partition header on disk #1: %s\n",
guid_to_str(&si->sph_memory->sadump_id, guid,
sizeof(guid)));
DEBUG_MSG(" partition header on disk #%d: %s\n", diskid,
guid_to_str(&sph->sadump_id, guid, sizeof(guid)));
goto error;
}
if (memcmp(&si->sph_memory->disk_set_id, &sph->disk_set_id,
sizeof(efi_guid_t)) != 0) {
DEBUG_MSG("sadump: disk set ID mismatch\n");
DEBUG_MSG(" partition header on disk #1: %s\n",
guid_to_str(&si->sph_memory->disk_set_id, guid,
sizeof(guid)));
DEBUG_MSG(" partition header on disk #%d: %s\n", diskid,
guid_to_str(&sph->disk_set_id, guid, sizeof(guid)));
goto error;
}
if (memcmp(&si->sdh_memory->vol_info[diskid-1].id, &sph->vol_id,
sizeof(efi_guid_t)) != 0) {
DEBUG_MSG("sadump: volume ID mismatch\n");
DEBUG_MSG(" disk set header on disk #1: %s\n",
guid_to_str(&si->sdh_memory->vol_info[diskid-1].id,
guid, sizeof(guid)));
DEBUG_MSG(" partition header on disk #%d: %s\n",
diskid+1,
guid_to_str(&sph->vol_id, guid, sizeof(guid)));
goto error;
}
if (memcmp(&si->sph_memory->time_stamp, &sph->time_stamp,
sizeof(efi_time_t)) != 0) {
DEBUG_MSG("sadump time stamp mismatch\n");
DEBUG_MSG(" partition header on disk #1: %s\n",
asctime(efi_time_t_to_tm
(&si->sph_memory->time_stamp)));
DEBUG_MSG(" partition header on disk #%d: %s\n",
diskid, asctime(efi_time_t_to_tm(&sph->time_stamp)));
}
if (diskid+1 != sph->set_disk_set) {
DEBUG_MSG("sadump: wrong disk order; #%d expected but #%d given\n",
diskid+1, sph->set_disk_set);
goto error;
}
sdi->sph_memory = sph;
sdi->data_offset = si->sh_memory->block_size;
return TRUE;
error:
free(sph);
return FALSE;
}
int
sadump_initialize_bitmap_memory(void)
{
struct sadump_header *sh = si->sh_memory;
struct dump_bitmap *bmp;
unsigned long dumpable_bitmap_offset;
unsigned long long section, max_section;
mdf_pfn_t pfn;
unsigned long long *block_table;
dumpable_bitmap_offset =
si->sub_hdr_offset +
sh->block_size * (sh->sub_hdr_size + sh->bitmap_blocks);
bmp = malloc(sizeof(struct dump_bitmap));
if (bmp == NULL) {
ERRMSG("Can't allocate memory for the memory-bitmap. %s\n",
strerror(errno));
return FALSE;
}
bmp->fd = info->fd_memory;
bmp->file_name = info->name_memory;
bmp->no_block = -1;
bmp->offset = dumpable_bitmap_offset;
bmp->buf = malloc(BUFSIZE_BITMAP);
if (!bmp->buf) {
ERRMSG("Can't allocate memory for the memory-bitmap's buffer. %s\n",
strerror(errno));
free(bmp);
return FALSE;
}
memset(bmp->buf, 0, BUFSIZE_BITMAP);
max_section = divideup(si->max_mapnr, SADUMP_PF_SECTION_NUM);
block_table = calloc(sizeof(unsigned long long), max_section);
if (block_table == NULL) {
ERRMSG("Can't allocate memory for the block_table. %s\n",
strerror(errno));
free(bmp->buf);
free(bmp);
return FALSE;
}
for (section = 0; section < max_section; ++section) {
if (section > 0)
block_table[section] = block_table[section-1];
for (pfn = section * SADUMP_PF_SECTION_NUM;
pfn < (section + 1) * SADUMP_PF_SECTION_NUM;
++pfn)
if (is_dumpable(bmp, pfn, NULL))
block_table[section]++;
}
info->bitmap_memory = bmp;
si->block_table = block_table;
bmp = malloc(sizeof(struct dump_bitmap));
if (bmp == NULL) {
ERRMSG("Can't allocate memory for the memory-bitmap. %s\n",
strerror(errno));
return FALSE;
}
bmp->fd = info->fd_memory;
bmp->file_name = info->name_memory;
bmp->no_block = -1;
bmp->offset = si->sub_hdr_offset + sh->block_size * sh->sub_hdr_size;
bmp->buf = malloc(BUFSIZE_BITMAP);
if (!bmp->buf) {
ERRMSG("Can't allocate memory for the memory-bitmap's buffer. %s\n",
strerror(errno));
free(bmp);
return FALSE;
}
memset(bmp->buf, 0, BUFSIZE_BITMAP);
si->ram_bitmap = bmp;
/*
* Perform explicitly zero filtering. Without this processing
* crash utility faces different behaviors on reading zero
* pages that are filtered out on the kdump-compressed format
* originating from kdump ELF and from sadump formats: the
* former succeeds in reading zero pages but the latter fails.
*/
for (pfn = 0; pfn < si->max_mapnr; pfn++) {
if (sadump_is_ram(pfn) &&
!sadump_is_dumpable(info->bitmap_memory, pfn)) {
info->dump_level |= DL_EXCLUDE_ZERO;
break;
}
}
return TRUE;
}
static int
max_mask_cpu(void)
{
return BITPERBYTE * si->cpumask_size;
}
static int
cpu_online_mask_init(void)
{
ulong cpu_online_mask_addr;
if (si->cpu_online_mask_buf && si->cpumask_size)
return TRUE;
if (SYMBOL(cpu_online_mask) == NOT_FOUND_SYMBOL ||
(SIZE(cpumask) == NOT_FOUND_STRUCTURE &&
SIZE(cpumask_t) == NOT_FOUND_STRUCTURE))
return FALSE;
si->cpumask_size = SIZE(cpumask) == NOT_FOUND_STRUCTURE
? SIZE(cpumask_t)
: SIZE(cpumask);
if (!(si->cpu_online_mask_buf = calloc(1, si->cpumask_size))) {
ERRMSG("Can't allocate cpu_online_mask buffer. %s\n",
strerror(errno));
return FALSE;
}
if ((SIZE(cpumask) == NOT_FOUND_STRUCTURE) ||
(SYMBOL(__cpu_online_mask) != NOT_FOUND_SYMBOL))
cpu_online_mask_addr = SYMBOL(cpu_online_mask);
else {
if (!readmem(VADDR, SYMBOL(cpu_online_mask),
&cpu_online_mask_addr, sizeof(unsigned long))) {
ERRMSG("Can't read cpu_online_mask pointer.\n");
return FALSE;
}
}
if (!readmem(VADDR, cpu_online_mask_addr, si->cpu_online_mask_buf,
si->cpumask_size)) {
ERRMSG("Can't read cpu_online_mask memory.\n");
return FALSE;
}
return TRUE;
}
int
sadump_num_online_cpus(void)
{
int cpu, count = 0;
if (!cpu_online_mask_init())
return FALSE;
DEBUG_MSG("sadump: online cpus:");
for_each_online_cpu(cpu) {
count++;
DEBUG_MSG(" %d", cpu);
}
DEBUG_MSG("\nsadump: nr_cpus: %d\n", count);
return count;
}
int
sadump_set_timestamp(struct timeval *ts)
{