added some comments
[tridge/junkcode.git] / tphdisk.c
1 /*
2   this creates a save2dsk.bin hibernation file for phoenix netbios
3   laptops. I tested it on my IBM Thinkpad T20 but it should work on
4   other laptops.
5
6   Copyright 2002 Andrew Tridgell <tridge@samba.org> 
7   released under the GNU General Public License version 2 or later
8 */
9
10 /*
11   This program was inspired by lphdisk which was written by Patrick
12   Ashmore and Alex Stewart. The main difference is that tphdisk
13   doesn't muck about with partition tables, which makes it easy to use
14   it for either a hibernation partition or a save2dsk.bin file.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20
21 typedef unsigned short u16;
22 typedef unsigned u32;
23
24 /*
25   write or die ...
26 */
27 static void x_write(int fd, void *data, size_t size)
28 {
29         if (write(fd, data, size) != (ssize_t)size) {
30                 fprintf(stderr,"Write failed!\n");
31                 exit(1);
32         }
33 }
34
35 /* 
36    the main function. The 'size' is in sectors (512 byte units) 
37
38    this writes a save2dsk.bin file to the given file descriptor
39 */
40 static void write_phdisk(int fd, u32 size)
41 {
42         unsigned char sector[512];
43         u16 checksum;
44         u32 i;
45
46         memset(sector, 0, 512);
47
48         /* form the header - this is x86 byte order dependent, but who
49            has a non-intel lapptop with a phoenix notebios? */
50         strcpy(sector, "TimO");
51         sector[12] = 2;
52         *(u32 *)&sector[16] = size;
53         for (checksum=0, i=8;i<512;i+=2) {
54                 checksum += *(u16 *)(&sector[i]);
55         }
56         *(u16 *)&sector[6] = ~checksum;
57
58         /* write two copies of the header */
59         x_write(fd, sector, 512);
60         x_write(fd, sector, 512);
61
62         /* rest is filled with 0x50 */
63         memset(sector, 0x50, 512);
64
65         for (i=2;i<size;i++) {
66                 x_write(fd, sector, 512);
67         }
68 }
69
70 /* main program */
71 int main(int argc, char *argv[])
72 {
73         int size;
74
75         if (argc < 2) {
76                 fprintf(stderr,"
77 Usage: tphdisk <size in MB>
78
79 written by Andrew Tridgell <tridge@samba.org>
80
81
82 This program writes a 'save2dsk.bin' hibernation file to stdout. To
83 use it you should do something like this:
84
85 1) create a type 16 (Hidden FAT16) partition on your laptop
86 2) format the partition with 'mkdosfs'
87 3) mount the partition as VFAT
88 4) create the 'save2dsk.bin' file on the partition using something like
89       tphdisk 280 > save2dsk.bin
90 5) Do a full reboot
91
92 The only parameter is the size in megabytes of the save file. This
93 needs to be at least as big as your main memory + video memory, but you
94 can make it larger if you want to.
95 ");
96                 exit(1);
97         }
98
99         size = atoi(argv[1]) * 1024 * 2;
100
101         write_phdisk(1, size);
102         return 0;
103 }