File: bootsched.c

package info (click to toggle)
powerpc-utils 1.1.3-22
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 280 kB
  • ctags: 382
  • sloc: ansic: 2,807; makefile: 174; sh: 58
file content (265 lines) | stat: -rw-r--r-- 5,186 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * bootsched.c - set the next power up time on VIA-CUDA.
 *
 * Debian version of pmacpow.c (essentially, renamed and docs added)
 * 2005-07-06 MSch
 *
 * Copyright (C) 1999 Takashi Oe.  All rights reserved.
 * Parts of the code are from clock.c and bat.c
 * by Paul Mackerras.
 *
 * Modified 2002 Keith Keller.
 * 
 * To make: save file to pmacpow.c
 * gcc -o pmacpow pmacpow.c
 * strip pmacpow
 *
 * 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; either version
 * 2 of the License, or (at your option) any later version.
 */

#include "autoboot-conf.h"

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <time.h>
#include <sys/time.h>

#ifdef HAVE_ASM_ADB_H
# include <asm/adb.h>
# else
#  ifdef HAVE_LINUX_ADB_H
#   include <linux/adb.h>
#  endif
#endif

#ifdef HAVE_ASM_CUDA_H
# include <asm/cuda.h>
# else
#  ifdef HAVE_LINUX_CUDA_H
#   include <linux/cuda.h>
#  endif
#endif

unsigned char reqt[2] =
	{ CUDA_PACKET, CUDA_GET_TIME};
unsigned char reqp[6] =
	{ CUDA_PACKET, CUDA_POWERUP_TIME, 0, 0, 0, 0};

unsigned char reply[16];

/* RTC on PowerMacs stores seconds since 1 Jan 1904 */
#define RTC_OFFSET	2082844800

static int week = 0;
static int day = -1;
static int abstime = 0;
static int quiet = 0;
static char *program_name;

void dump(unsigned char *buf, int n)
{
	int i;

	for (i = 0; i < n; ++i)
		fprintf(stderr, " %.2x", buf[i]);
	fprintf(stderr, "\n");
}

void usage(void) {
	fprintf(stderr,
		"%s [-q] [-w|-e|-d DAY] -t HH:MM\n"
		"%s [-q] +value[mhd]\n"
		"\t-w: set next power-up on a weekday\n"
		"\t-e: set next power-up on a weekend day\n"
		"\t-d DAY: set next power-up to DAY (0..6);"
			" 0 represents Sunday\n"
		"\t-q: quiet mode\n"
		"\t-t HH:MM: time of day to power-up\n"
		"\t+value[mhd]: time in secs, mins, hours, or days ahead of"
			" current time\n",
		program_name, program_name);
	exit(1);
}

int next_wday(int wday) {
	if (wday == 0)
		return 1;
	if (wday == 6)
		return 2;
	return 0;
}

int next_weday(int wday) {
	if ((wday >=1) && (wday < 6))
		return 6 - wday;
	return 0;
}

int set_powerup_time(int fd, long up)
{
	int n;

	*(long *)(reqp+2) = up;
	n = write(fd, reqp, sizeof(reqp));
	if (n != sizeof(reqp)) {
		fprintf(stderr, "%s: write returned %d\n", program_name, n);
		return -1;
	}
	if ((n = read(fd, reply, sizeof(reply))) < 0) {
		perror("read");
		return -1;
	}
	if (n != 3) {
		dump(reply, n);
		fprintf(stderr, "%s: command failed\n", program_name);
		return -1;
	}
	return 0;
}

long get_current_time(int fd)
{
	int n;

	n = write(fd, reqt, sizeof(reqt));
	if (n != sizeof(reqt)) {
		fprintf(stderr, "%s: write returned %d\n", program_name, n);
		return -1;
	}
	if ((n = read(fd, reply, sizeof(reply))) < 0) {
		perror("read");
		return -1;
	}
	if (n != 7) {
		dump(reply, n);
		fprintf(stderr, "%s: command failed\n", program_name);
		return -1;
	}
	return (long) ((reply[3] << 24) + (reply[4] << 16)
			+ (reply[5] << 8) + (long) reply[6]);
}

long calc_powerup_time(hour, min)
{
	struct tm tm, ctm;
	time_t systime;

	systime = time(NULL);
	ctm = *localtime(&systime);
	tm = ctm;
	if (!quiet)
		fprintf(stderr, "current time is %s", asctime(&tm));
	tm.tm_sec = 0;
	tm.tm_min = min;
	tm.tm_hour = hour;
	if (mktime(&tm) < (systime + 60)) {
		++tm.tm_mday;
		++tm.tm_wday;
		(void)mktime(&tm);
	}
	if (day == -1) {
		if (week == 1)
			tm.tm_mday += next_wday(tm.tm_wday);
		if (week == 2)
			tm.tm_mday += next_weday(tm.tm_wday);
	} else {
		if (day != tm.tm_wday) {
			if (day < tm.tm_wday)
				day += 7;
			tm.tm_mday += day - tm.tm_wday;
		}
	}
	return (long)(mktime(&tm) + RTC_OFFSET);
}

int main(int argc, char **argv)
{
	int fd, arg, hour, min;
	long sec, uptime;
	char *timestr, *tmp;
	extern int optind;

	program_name = (char *)basename(argv[0]);
	if (getuid()) {
		fprintf(stderr, "Sorry, must be root to set power up time\n");
		return 1;
	}
	while ((arg = getopt(argc, argv, "wed:tq")) != EOF) {
		switch (arg) {
		case 0:
			break;
		case 'w':
			week = 1;
			break;
		case 'e':
			week = 2;
			break;
		case 'd':
			day = atoi(optarg);
			if ((day < 0) || (day > 6))
				usage();
			break;
		case 't':
			abstime = 1;
			break;
		case 'q':
			quiet = 1;
			break;
		default:
			usage();
		}
	}
	if (argv[optind])
		timestr = argv[optind];
	else
		usage();
	tmp = strsep(&timestr, ":");
	if (tmp && abstime) {
		if (!tmp || !timestr)
			usage();
		hour = atoi(tmp);
		min = atoi(timestr);
		sec = 0;
	} else {
		timestr = argv[optind];
		sec = strtol(timestr, &tmp, 10);
		if (tmp) {
			switch (tolower(tmp[0])) {
			case 'd':
				sec *= 24;
			case 'h':
				sec *= 60;
			case 'm':
				sec *= 60;
				break;
			}
		}
	}

	if ((fd = open("/dev/adb", O_RDWR)) < 0) {
		perror("open");
		return 1;
	}
	if (sec > 0) {
		long cur = get_current_time(fd);

		if (cur == -1)
			return 1;
		uptime = cur + sec;
	} else
		uptime = calc_powerup_time(hour, min);
	if (set_powerup_time(fd, uptime) < 0)
		return 1;
	close(fd);
	if (!quiet) {
		uptime -= RTC_OFFSET;
		fprintf(stderr, "will be up again %s",
			asctime(localtime(&uptime)));
	}
	return 0;
}