2 * Copyright (c) 2002 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
38 #include <machine/efi.h>
39 #include <machine/elf.h>
40 #include <machine/md_var.h>
42 CTASSERT(sizeof(struct kerneldumpheader) == 512);
45 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
46 * is to protect us from metadata and to protect metadata from us.
48 #define SIZEOF_METADATA (64*1024)
50 #define MD_ALIGN(x) (((off_t)(x) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK)
51 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
53 typedef int callback_t(struct efi_md*, int, void*);
55 static struct kerneldumpheader kdh;
56 static off_t dumplo, fileofs;
58 /* Handle buffered writes. */
59 static char buffer[DEV_BSIZE];
63 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
69 len = DEV_BSIZE - fragsz;
72 bcopy(ptr, buffer + fragsz, len);
76 if (fragsz == DEV_BSIZE) {
77 error = dump_write(di, buffer, 0, dumplo,
90 buf_flush(struct dumperinfo *di)
97 error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
104 cb_dumpdata(struct efi_md *mdp, int seqnr, void *arg)
106 struct dumperinfo *di = (struct dumperinfo*)arg;
110 int c, error, twiddle;
112 error = 0; /* catch case in which mdp->md_pages is 0 */
113 counter = 0; /* Update twiddle every 16MB */
116 pa = IA64_PHYS_TO_RR7(mdp->md_phys);
118 printf(" chunk %d: %ld pages ", seqnr, (long)pgs);
121 sz = (pgs > (DFLTPHYS >> EFI_PAGE_SHIFT))
122 ? DFLTPHYS : pgs << EFI_PAGE_SHIFT;
125 printf("%c\b", "|/-\\"[twiddle++ & 3]);
126 counter &= (1<<24) - 1;
128 error = dump_write(di, (void*)pa, 0, dumplo, sz);
132 pgs -= sz >> EFI_PAGE_SHIFT;
135 /* Check for user abort. */
140 printf("(CTRL-C to abort) ");
142 printf("... %s\n", (error) ? "fail" : "ok");
147 cb_dumphdr(struct efi_md *mdp, int seqnr, void *arg)
149 struct dumperinfo *di = (struct dumperinfo*)arg;
153 bzero(&phdr, sizeof(phdr));
154 phdr.p_type = PT_LOAD;
155 phdr.p_flags = PF_R; /* XXX */
156 phdr.p_offset = fileofs;
157 phdr.p_vaddr = (uintptr_t)mdp->md_virt; /* XXX probably bogus. */
158 phdr.p_paddr = mdp->md_phys;
159 phdr.p_filesz = mdp->md_pages << EFI_PAGE_SHIFT;
160 phdr.p_memsz = mdp->md_pages << EFI_PAGE_SHIFT;
161 phdr.p_align = EFI_PAGE_SIZE;
163 error = buf_write(di, (char*)&phdr, sizeof(phdr));
164 fileofs += phdr.p_filesz;
169 cb_size(struct efi_md *mdp, int seqnr, void *arg)
171 uint64_t *sz = (uint64_t*)arg;
173 *sz += (uint64_t)mdp->md_pages << EFI_PAGE_SHIFT;
178 foreach_chunk(callback_t cb, void *arg)
184 mdp = efi_md_first();
185 while (mdp != NULL) {
186 if (mdp->md_type == EFI_MD_TYPE_FREE) {
187 error = (*cb)(mdp, seqnr++, arg);
191 mdp = efi_md_next(mdp);
197 dumpsys(struct dumperinfo *di)
205 bzero(&ehdr, sizeof(ehdr));
206 ehdr.e_ident[EI_MAG0] = ELFMAG0;
207 ehdr.e_ident[EI_MAG1] = ELFMAG1;
208 ehdr.e_ident[EI_MAG2] = ELFMAG2;
209 ehdr.e_ident[EI_MAG3] = ELFMAG3;
210 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
211 #if BYTE_ORDER == LITTLE_ENDIAN
212 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
214 ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
216 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
217 ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE; /* XXX big picture? */
218 ehdr.e_type = ET_CORE;
219 ehdr.e_machine = EM_IA_64;
220 ehdr.e_phoff = sizeof(ehdr);
221 ehdr.e_flags = EF_IA_64_ABSOLUTE; /* XXX misuse? */
222 ehdr.e_ehsize = sizeof(ehdr);
223 ehdr.e_phentsize = sizeof(Elf64_Phdr);
224 ehdr.e_shentsize = sizeof(Elf64_Shdr);
226 /* Calculate dump size. */
228 ehdr.e_phnum = foreach_chunk(cb_size, &dumpsize);
229 hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
230 fileofs = MD_ALIGN(hdrsz);
232 hdrgap = fileofs - DEV_ALIGN(hdrsz);
234 /* Determine dump offset on device. */
235 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
239 dumplo = di->mediaoffset + di->mediasize - dumpsize;
240 dumplo -= sizeof(kdh) * 2;
242 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_IA64_VERSION, dumpsize, di->blocksize);
244 printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20,
248 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
251 dumplo += sizeof(kdh);
253 /* Dump ELF header */
254 error = buf_write(di, (char*)&ehdr, sizeof(ehdr));
258 /* Dump program headers */
259 error = foreach_chunk(cb_dumphdr, di);
265 * All headers are written using blocked I/O, so we know the
266 * current offset is (still) block aligned. Skip the alignement
267 * in the file to have the segment contents aligned at page
268 * boundary. We cannot use MD_ALIGN on dumplo, because we don't
269 * care and may very well be unaligned within the dump device.
273 /* Dump memory chunks (updates dumplo) */
274 error = foreach_chunk(cb_dumpdata, di);
279 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
283 /* Signal completion, signoff and exit stage left. */
284 dump_write(di, NULL, 0, 0, 0);
285 printf("\nDump complete\n");
292 if (error == ECANCELED)
293 printf("\nDump aborted\n");
295 printf("\n** DUMP FAILED (ERROR %d) **\n", error);