mbr/Makefile: run checksize on the proper binaries...
[syslinux.git] / memdump / main.c
blobf408926eb588c29d3a2d4821d7bae20ccfe47c19
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 * ----------------------------------------------------------------------- */
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "mystuff.h"
17 #include "ymsend.h"
18 #include "io.h"
20 const char *program = "memdump";
22 void __attribute__((noreturn)) die(const char *msg)
24 puts(program);
25 puts(": ");
26 puts(msg);
27 putchar('\n');
28 exit(1);
31 #ifdef DEBUG
32 # define dprintf printf
33 #else
34 # define dprintf(...) ((void)0)
35 #endif
37 static inline __attribute__((const)) uint16_t ds(void)
39 uint16_t v;
40 asm("movw %%ds,%0" : "=rm" (v));
41 return 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,
52 size_t pos)
54 size_t end;
55 static uint64_t gdt[6];
56 size_t bufl;
58 pos += (size_t)finfo->pvt; /* Add base */
59 end = pos+len;
61 if (end <= 0x100000) {
62 /* Can stay in real mode */
63 asm volatile("movw %3,%%fs ; "
64 "fs; rep; movsl ; "
65 "movw %2,%%cx ; "
66 "rep; movsb"
67 : : "D" (buf), "c" (len >> 2), "r" ((uint16_t)(len & 3)),
68 "rm" ((uint16_t)(pos >> 4)), "S" (pos & 15)
69 : "memory");
70 } else {
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)
76 : "memory");
80 int main(int argc, char *argv[])
82 uint16_t bios_ports[4];
83 const char *prefix;
84 char filename[1024];
85 int i;
86 static struct serial_if sif =
88 .read = serial_read,
89 .write = serial_write,
91 struct file_info finfo;
92 const char serial_banner[] = "Now begin Ymodem download...\r\n";
94 if (argc < 4)
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);
104 if (sif.port <= 3) {
105 sif.port = bios_ports[sif.port];
108 if (serial_init(&sif))
109 die("failed to initialize serial port");
111 prefix = argv[2];
113 puts("Printing prefix...\n");
114 sif.write(&sif, serial_banner, sizeof serial_banner-1);
116 for (i = 3; i < argc; i++) {
117 uint32_t start, len;
118 char *ep;
120 start = strtoul(argv[i], &ep, 0);
121 if (*ep != ',')
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;
127 finfo.size = len;
128 finfo.pvt = (void *)start;
130 puts("Sending ");
131 puts(filename);
132 puts("...\n");
134 send_ymodem(&sif, &finfo, get_bytes);
137 puts("Sending closing signature...\n");
138 end_ymodem(&sif);
140 return 0;