aboutsummaryrefslogtreecommitdiffstats
path: root/com32/sysdump/main.c
blob: 5aba85eaf7f0e02bf211ad4d239efc30a2a70970 (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   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 <inttypes.h>
#include <dprintf.h>
#include "ymsend.h"
#include "srecsend.h"

const char *program = "memdump";

void __attribute__ ((noreturn)) die(const char *msg)
{
    printf("%s: %s\n", program, msg);
    exit(1);
}

static void get_bytes(void *buf, size_t len, struct file_info *finfo,
		      size_t pos)
{
    pos += (size_t) finfo->pvt;	/* Add base */
    memcpy(buf, (void *)pos, len);
}

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...");
	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;

	printf("Sending %s...\n", filename);

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

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

    return 0;
}