aboutsummaryrefslogtreecommitdiff
path: root/CS3871/os/copy.c
blob: 6e21083b347b23b6bcaca2a56b94e894d39190c3 (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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

int main(argc, argv)
int argc;
char ** argv;
{
	int val;
	char buf[512], *dev;
	int file, floppy;

	if (argc < 3 ) {
		perror("usage: copy path_to_bootloader path_to_os [path_to_device]\n");
		exit(1);
	}
	if (argc >= 4) {
		dev = argv[3];
	} else {
		dev = "/dev/fd0";
	}

	memset(buf, 0, 512);

	floppy = open(dev, O_RDWR);
	if (file < 0) {
		perror("Could not open floppy: ");
		exit(1);
	}

	/******************************
	 * Write Bootloader to Floppy *
	 ******************************/

	file = open(argv[1], O_RDONLY);
	if (file < 0) {
		perror("Could not open bootloader: ");
		exit(1);
	}

	val = read(file, buf, 510);
	close(file);
	if (val < 0) {
		perror("Could not read bootloader: ");
		exit(1);
	}

	buf[510] = 0x55;
	buf[511] = 0xaa;

	lseek(floppy, 0, SEEK_CUR);

	val = write(floppy, buf, 512);
	if (val < 0) {
		perror("Error writing to floppy: ");
		close(floppy);
		exit(1);
	}
	

	/**********************
 	 * Write OS to Floppy *
	 **********************/

	file = open(argv[2], O_RDONLY);
	if (file < 0) {
		perror("Could not open os: ");
		exit(1);
	}

	val = read(file, buf, 512);
	close(file);
	if (val < 0) {
		perror("Could not read os: ");
		exit(1);
	}

	val = write(floppy, buf, 512);
	if (val < 0) {
		perror("Error writing to floppy: ");
		close(floppy);
		exit(1);
	}

	syncfs(floppy);
	close(floppy);

	printf("Floppy successfully copied!\n");
	return 0;
}