~ubuntu-branches/ubuntu/feisty/powerpc-utils/feisty

« back to all changes in this revision

Viewing changes to autoboot.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Schmitz
  • Date: 2005-05-09 12:31:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050509123124-m6cnwua15r7say20
Tags: 1.1.3-15
Merge back patches from ubuntu (thanks to Adam Conrad <adconrad@0c3.net>
for the hint): fix linuxdoc-tools b-d, drop adb_mouse.h header. Update 
description to include autoboot and lsprop tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * autoboot.c - set/clear Server Mode boot on the VIA-CUDA
 
3
 *
 
4
 *
 
5
 * Copyright (C) 2005 Rich Johnson.  All rights rserved.
 
6
 *
 
7
 * This code is modeled on pmacpow, written by Takashi Oe
 
8
 * and modified by Keith Keller.
 
9
 * 
 
10
 * To make: save file to autoboot.c
 
11
 * gcc -o autoboot  autoboot.c
 
12
 * strip autoboot
 
13
 *
 
14
 * This program is free software; you can redistribute it and/or
 
15
 * modify it under the terms of the GNU General Public License
 
16
 * as published by the Free Software Foundation; either version
 
17
 * 2 of the License, or (at your option) any later version.
 
18
 *
 
19
 * Setting Server Mode causes the machine to boot automatically
 
20
 * upon restoration of power after an outage.
 
21
 * Clearing Server Mode will requires a manual boot.
 
22
 * Normal shutdown and sheduling a boot (see bootsched) clear
 
23
 * Server Mode.
 
24
 *
 
25
 * The server mode setting is good for the current OS session only
 
26
 * and is cleared when the machine boots.  For continuous
 
27
 * unattended operation autoboot should be invoked during the boot 
 
28
 * process.
 
29
 *
 
30
 * see also: bootsched.
 
31
 */
 
32
 
 
33
#include "autoboot-conf.h"
 
34
 
 
35
#include <stdio.h>
 
36
#include <string.h>
 
37
#include <fcntl.h>
 
38
#include <getopt.h>
 
39
#include <time.h>
 
40
#include <sys/time.h>
 
41
 
 
42
#ifdef HAVE_ASM_ADB_H
 
43
# include <asm/adb.h>
 
44
# else
 
45
#  ifdef HAVE_LINUX_ADB_H
 
46
#   include <linux/adb.h>
 
47
#  endif
 
48
#endif
 
49
 
 
50
#ifdef HAVE_ASM_CUDA_H
 
51
# include <asm/cuda.h>
 
52
# else
 
53
#  ifdef HAVE_LINUX_CUDA_H
 
54
#   include <linux/cuda.h>
 
55
#  endif
 
56
#endif
 
57
 
 
58
unsigned char reqt[2] =
 
59
        { CUDA_PACKET, CUDA_GET_TIME};
 
60
unsigned char reqp[6] =
 
61
        { CUDA_PACKET, CUDA_POWERUP_TIME, 0, 0, 0, 0};
 
62
unsigned char server[3] =
 
63
        { CUDA_PACKET, 0x13, 0 };
 
64
 
 
65
unsigned char reply[16];
 
66
 
 
67
 
 
68
static int verbose = 0;
 
69
static int version  = 0;
 
70
static int synopsis  = 0;
 
71
static int servermode = 0;
 
72
static char *program_name;
 
73
 
 
74
void dump(unsigned char *buf, int n)
 
75
{
 
76
        int i;
 
77
 
 
78
        for (i = 0; i < n; ++i)
 
79
                fprintf(stderr, " %.2x", buf[i]);
 
80
        fprintf(stderr, "\n");
 
81
}
 
82
 
 
83
void usage(void) {
 
84
        fprintf(stderr,
 
85
                "%s [-v] on\n"
 
86
                "%s [-v] off\n"
 
87
                "%s -V\n"
 
88
                "%s -?\n"
 
89
                "\t-v: verbose operation\n"
 
90
                "\t-V: print version number and exit \n"
 
91
                "\t-?: print this synopsis\n"
 
92
                "\ton: enable server mode, boot whenever power restored\n"
 
93
                "\toff: disable server mode, boot manually\n",
 
94
                program_name, program_name, program_name, program_name );
 
95
        exit(1);
 
96
}
 
97
 
 
98
 
 
99
void print_version( void ){
 
100
        fprintf( stderr, "%s: v1.0\n", program_name );
 
101
        exit(0);
 
102
        };
 
103
 
 
104
int set_servermode( int fd, long is_server, int is_verbose )
 
105
{
 
106
        int n;
 
107
 
 
108
        server[2]        = 0;
 
109
        if( is_server != 0 )
 
110
                server[2]       = 1;
 
111
        n = write(fd, server, sizeof(server));
 
112
        if (n != sizeof(server)) {
 
113
                fprintf(stderr, "%s: write returned %d\n", program_name, n);
 
114
                return -1;
 
115
        }
 
116
        if( is_verbose ){
 
117
                if( is_server ){
 
118
                        fprintf(stderr, "Automatic boot enabled (Server Mode)\n");
 
119
                }
 
120
                  else{
 
121
                        fprintf(stderr, "Automatic boot disabled \n");
 
122
                }
 
123
        }
 
124
        return 0;
 
125
}
 
126
 
 
127
extern int optind;
 
128
 
 
129
int main(int argc, char **argv)
 
130
{
 
131
        int fd, arg, hour, min;
 
132
        long sec, uptime;
 
133
        char *timestr, *tmp;
 
134
 
 
135
        program_name = (char *)basename(argv[0]);
 
136
        if (getuid()) {
 
137
                fprintf(stderr, "Sorry, must be root to set power up time\n");
 
138
                return 1;
 
139
        }
 
140
        while ((arg = getopt(argc, argv, "vV")) != EOF) {
 
141
                switch (arg) {
 
142
                case 0:
 
143
                        break;
 
144
                case '?':
 
145
                        synopsis = 1;
 
146
                        break;
 
147
                case 'V':
 
148
                        version = 1;
 
149
                        break;
 
150
                case 'v':
 
151
                        verbose = 1;
 
152
                        break;
 
153
                default:
 
154
                        usage();
 
155
                }
 
156
        }
 
157
 
 
158
        if(synopsis)
 
159
                usage();
 
160
        if (version){
 
161
                if (optind != argc)
 
162
                        usage();
 
163
                print_version();
 
164
        }
 
165
        if (optind != argc-1)
 
166
                usage();
 
167
 
 
168
        if( strcmp( argv[optind], "on" ) == 0 ){
 
169
                servermode = 1;
 
170
        }
 
171
        else if( strcmp( argv[optind], "off" ) == 0 ){
 
172
                servermode = 0;
 
173
        }
 
174
        else{
 
175
                usage();
 
176
        }
 
177
        
 
178
        if ((fd = open("/dev/adb", O_RDWR)) < 0) {
 
179
                perror("open");
 
180
                return 1;
 
181
        }
 
182
        set_servermode( fd, servermode, verbose );
 
183
        close(fd);
 
184
        return 0;
 
185
}