File: main.c

package info (click to toggle)
syslinux 2%3A4.05%2Bdfsg-6%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 25,684 kB
  • sloc: ansic: 276,159; asm: 9,973; pascal: 9,907; perl: 3,492; makefile: 1,736; sh: 626; python: 266; xml: 39
file content (157 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download | duplicates (2)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *
 *   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, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "mystuff.h"
#include "ymsend.h"
#include "srecsend.h"
#include "io.h"

const char *program = "memdump";

void __attribute__ ((noreturn)) die(const char *msg)
{
    puts(program);
    puts(": ");
    puts(msg);
    putchar('\n');
    exit(1);
}

#ifdef DEBUG
# define dprintf printf
#else
# define dprintf(...) ((void)0)
#endif

static inline __attribute__ ((const))
uint16_t ds(void)
{
    uint16_t v;
asm("movw %%ds,%0":"=rm"(v));
    return v;
}

#define GDT_ENTRY(flags,base,limit)		\
	(((uint64_t)(base & 0xff000000) << 32) |	\
	 ((uint64_t)flags << 40) |			\
	 ((uint64_t)(limit & 0x00ff0000) << 32) |	\
	 ((uint64_t)(base & 0x00ffff00) << 16) |	\
	 ((uint64_t)(limit & 0x0000ffff)))

static void get_bytes(void *buf, size_t len, struct file_info *finfo,
		      size_t pos)
{
    size_t end;
    static uint64_t gdt[6];
    size_t bufl;

    pos += (size_t) finfo->pvt;	/* Add base */
    end = pos + len;

    if (end <= 0x100000) {
	/* Can stay in real mode */
	asm volatile ("movw %3,%%fs ; "
		      "fs; rep; movsl ; "
		      "movw %2,%%cx ; "
		      "rep; movsb"::"D" (buf), "c"(len >> 2),
		      "r"((uint16_t) (len & 3)), "rm"((uint16_t) (pos >> 4)),
		      "S"(pos & 15)
		      :"memory");
    } else {
	bufl = (ds() << 4) + (size_t) buf;
	gdt[2] = GDT_ENTRY(0x0093, pos, 0xffff);
	gdt[3] = GDT_ENTRY(0x0093, bufl, 0xffff);
	asm volatile ("pushal ; int $0x15 ; popal"::"a" (0x8700),
		      "c"((len + 1) >> 1), "S"(&gdt)
		      :"memory");
    }
}

int main(int argc, char *argv[])
{
    uint16_t bios_ports[4];
    const char *prefix;
    char filename[1024];
    int i;
    static struct serial_if sif = {
	.read = serial_read,
	.write = serial_write,
    };
    struct file_info finfo;
    const char ymodem_banner[] = "Now begin Ymodem download...\r\n";
    bool srec = false;

    if (argv[1][0] == '-') {
	srec = argv[1][1] == 's';
	argc--;
	argv++;
    }

    if (argc < 4)
	die("usage: memdump [-s] port prefix start,len...");

    finfo.pvt = (void *)0x400;
    get_bytes(bios_ports, 8, &finfo, 0);	/* Get BIOS serial ports */

    for (i = 0; i < 4; i++)
	printf("ttyS%i (COM%i) is at %#x\n", i, i + 1, bios_ports[i]);

    sif.port = strtoul(argv[1], NULL, 0);
    if (sif.port <= 3) {
	sif.port = bios_ports[sif.port];
    }

    if (serial_init(&sif))
	die("failed to initialize serial port");

    prefix = argv[2];

    if (!srec) {
	puts("Printing prefix...\n");
	sif.write(&sif, ymodem_banner, sizeof ymodem_banner - 1);
    }

    for (i = 3; i < argc; i++) {
	uint32_t start, len;
	char *ep;

	start = strtoul(argv[i], &ep, 0);
	if (*ep != ',')
	    die("invalid range specification");
	len = strtoul(ep + 1, NULL, 0);

	sprintf(filename, "%s%#x-%#x.bin", prefix, start, len);
	finfo.name = filename;
	finfo.size = len;
	finfo.pvt = (void *)start;

	puts("Sending ");
	puts(filename);
	puts("...\n");

	if (srec)
	    send_srec(&sif, &finfo, get_bytes);
	else
	    send_ymodem(&sif, &finfo, get_bytes);
    }

    if (!srec) {
	puts("Sending closing signature...\n");
	end_ymodem(&sif);
    }

    return 0;
}