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 * ----------------------------------------------------------------------- */
22 const char *program
= "memdump";
24 void __attribute__ ((noreturn
)) die(const char *msg
)
34 # define dprintf printf
36 # define dprintf(...) ((void)0)
39 static inline __attribute__ ((const))
43 asm("movw %%ds,%0":"=rm"(v
));
47 #define GDT_ENTRY(flags,base,limit) \
48 (((uint64_t)(base & 0xff000000) << 32) | \
49 ((uint64_t)flags << 40) | \
50 ((uint64_t)(limit & 0x00ff0000) << 32) | \
51 ((uint64_t)(base & 0x00ffff00) << 16) | \
52 ((uint64_t)(limit & 0x0000ffff)))
54 static void get_bytes(void *buf
, size_t len
, struct file_info
*finfo
,
58 static uint64_t gdt
[6];
61 pos
+= (size_t) finfo
->pvt
; /* Add base */
64 if (end
<= 0x100000) {
65 /* Can stay in real mode */
66 asm volatile ("movw %3,%%fs ; "
69 "rep; movsb"::"D" (buf
), "c"(len
>> 2),
70 "r"((uint16_t) (len
& 3)), "rm"((uint16_t) (pos
>> 4)),
74 bufl
= (ds() << 4) + (size_t) buf
;
75 gdt
[2] = GDT_ENTRY(0x0093, pos
, 0xffff);
76 gdt
[3] = GDT_ENTRY(0x0093, bufl
, 0xffff);
77 asm volatile ("pushal ; int $0x15 ; popal"::"a" (0x8700),
78 "c"((len
+ 1) >> 1), "S"(&gdt
)
83 int main(int argc
, char *argv
[])
85 uint16_t bios_ports
[4];
89 static struct serial_if sif
= {
91 .write
= serial_write
,
93 struct file_info finfo
;
94 const char ymodem_banner
[] = "Now begin Ymodem download...\r\n";
97 if (argv
[1][0] == '-') {
98 srec
= argv
[1][1] == 's';
104 die("usage: memdump [-s] port prefix start,len...");
106 finfo
.pvt
= (void *)0x400;
107 get_bytes(bios_ports
, 8, &finfo
, 0); /* Get BIOS serial ports */
109 for (i
= 0; i
< 4; i
++)
110 printf("ttyS%i (COM%i) is at %#x\n", i
, i
+ 1, bios_ports
[i
]);
112 sif
.port
= strtoul(argv
[1], NULL
, 0);
114 sif
.port
= bios_ports
[sif
.port
];
117 if (serial_init(&sif
))
118 die("failed to initialize serial port");
123 puts("Printing prefix...\n");
124 sif
.write(&sif
, ymodem_banner
, sizeof ymodem_banner
- 1);
127 for (i
= 3; i
< argc
; i
++) {
131 start
= strtoul(argv
[i
], &ep
, 0);
133 die("invalid range specification");
134 len
= strtoul(ep
+ 1, NULL
, 0);
136 sprintf(filename
, "%s%#x-%#x.bin", prefix
, start
, len
);
137 finfo
.name
= filename
;
139 finfo
.pvt
= (void *)start
;
146 send_srec(&sif
, &finfo
, get_bytes
);
148 send_ymodem(&sif
, &finfo
, get_bytes
);
152 puts("Sending closing signature...\n");