1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
20 const char *program
= "memdump";
22 void __attribute__((noreturn
)) die(const char *msg
)
32 # define dprintf printf
34 # define dprintf(...) ((void)0)
37 static inline __attribute__((const)) uint16_t ds(void)
40 asm("movw %%ds,%0" : "=rm" (v
));
44 #define GDT_ENTRY(flags,base,limit) \
45 (((uint64_t)(base & 0xff000000) << 32) | \
46 ((uint64_t)flags << 40) | \
47 ((uint64_t)(limit & 0x00ff0000) << 32) | \
48 ((uint64_t)(base & 0x00ffff00) << 16) | \
49 ((uint64_t)(limit & 0x0000ffff)))
51 static void get_bytes(void *buf
, size_t len
, struct file_info
*finfo
,
55 static uint64_t gdt
[6];
58 pos
+= (size_t)finfo
->pvt
; /* Add base */
61 if (end
<= 0x100000) {
62 /* Can stay in real mode */
63 asm volatile("movw %3,%%fs ; "
67 : : "D" (buf
), "c" (len
>> 2), "r" ((uint16_t)(len
& 3)),
68 "rm" ((uint16_t)(pos
>> 4)), "S" (pos
& 15)
71 bufl
= (ds() << 4)+(size_t)buf
;
72 gdt
[2] = GDT_ENTRY(0x0093, pos
, 0xffff);
73 gdt
[3] = GDT_ENTRY(0x0093, bufl
, 0xffff);
74 asm volatile("pushal ; int $0x15 ; popal"
75 : : "a" (0x8700), "c" ((len
+1) >> 1), "S" (&gdt
)
80 int main(int argc
, char *argv
[])
82 uint16_t bios_ports
[4];
86 static struct serial_if sif
=
89 .write
= serial_write
,
91 struct file_info finfo
;
92 const char serial_banner
[] = "Now begin Ymodem download...\r\n";
95 die("usage: memdump port prefix start,len...");
97 finfo
.pvt
= (void *)0x400;
98 get_bytes(bios_ports
, 8, &finfo
, 0); /* Get BIOS serial ports */
100 for (i
= 0; i
< 4; i
++)
101 printf("ttyS%i (COM%i) is at %#x\n", i
, i
+1, bios_ports
[i
]);
103 sif
.port
= strtoul(argv
[1], NULL
, 0);
105 sif
.port
= bios_ports
[sif
.port
];
108 if (serial_init(&sif
))
109 die("failed to initialize serial port");
113 puts("Printing prefix...\n");
114 sif
.write(&sif
, serial_banner
, sizeof serial_banner
-1);
116 for (i
= 3; i
< argc
; i
++) {
120 start
= strtoul(argv
[i
], &ep
, 0);
122 die("invalid range specification");
123 len
= strtoul(ep
+1, NULL
, 0);
125 sprintf(filename
, "%s%#x-%#x.bin", prefix
, start
, len
);
126 finfo
.name
= filename
;
128 finfo
.pvt
= (void *)start
;
134 send_ymodem(&sif
, &finfo
, get_bytes
);
137 puts("Sending closing signature...\n");