summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_ui.c
blob: 76152f3ff3ab76a9ab0b0c2af27efdf2f3f358bc (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
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
/*
 * kernel/power/tuxonice_ui.c
 *
 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
 * Copyright (C) 1998,2001,2002 Pavel Machek <pavel@suse.cz>
 * Copyright (C) 2002-2003 Florent Chabaud <fchabaud@free.fr>
 * Copyright (C) 2002-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * This file is released under the GPLv2.
 *
 * Routines for TuxOnIce's user interface.
 *
 * The user interface code talks to a userspace program via a
 * netlink socket.
 *
 * The kernel side:
 * - starts the userui program;
 * - sends text messages and progress bar status;
 *
 * The user space side:
 * - passes messages regarding user requests (abort, toggle reboot etc)
 *
 */

#define __KERNEL_SYSCALLS__

#include <linux/reboot.h>

#include "tuxonice_sysfs.h"
#include "tuxonice_modules.h"
#include "tuxonice.h"
#include "tuxonice_ui.h"
#include "tuxonice_netlink.h"
#include "tuxonice_power_off.h"
#include "tuxonice_builtin.h"

static char local_printf_buf[1024];        /* Same as printk - should be safe */
struct ui_ops *toi_current_ui;

/**
 * toi_wait_for_keypress - Wait for keypress via userui or /dev/console.
 *
 * @timeout: Maximum time to wait.
 *
 * Wait for a keypress, either from userui or /dev/console if userui isn't
 * available. The non-userui path is particularly for at boot-time, prior
 * to userui being started, when we have an important warning to give to
 * the user.
 */
static char toi_wait_for_keypress(int timeout)
{
        if (toi_current_ui && toi_current_ui->wait_for_key(timeout))
                return ' ';

        return toi_wait_for_keypress_dev_console(timeout);
}

/* toi_early_boot_message()
 * Description:        Handle errors early in the process of booting.
 *                 The user may press C to continue booting, perhaps
 *                 invalidating the image,  or space to reboot.
 *                 This works from either the serial console or normally
 *                 attached keyboard.
 *
 *                 Note that we come in here from init, while the kernel is
 *                 locked. If we want to get events from the serial console,
 *                 we need to temporarily unlock the kernel.
 *
 *                 toi_early_boot_message may also be called post-boot.
 *                 In this case, it simply printks the message and returns.
 *
 * Arguments:        int        Whether we are able to erase the image.
 *                 int        default_answer. What to do when we timeout. This
 *                         will normally be continue, but the user might
 *                         provide command line options (__setup) to override
 *                         particular cases.
 *                 Char *. Pointer to a string explaining why we're moaning.
 */

#define say(message, a...) printk(KERN_EMERG message, ##a)

void toi_early_boot_message(int message_detail, int default_answer,
        char *warning_reason, ...)
{
#if defined(CONFIG_VT) || defined(CONFIG_SERIAL_CONSOLE)
        unsigned long orig_state = get_toi_state(), continue_req = 0;
        unsigned long orig_loglevel = console_loglevel;
        int can_ask = 1;
#else
        int can_ask = 0;
#endif

        va_list args;
        int printed_len;

        if (!toi_wait) {
                set_toi_state(TOI_CONTINUE_REQ);
                can_ask = 0;
        }

        if (warning_reason) {
                va_start(args, warning_reason);
                printed_len = vsnprintf(local_printf_buf,
                                sizeof(local_printf_buf),
                                warning_reason,
                                args);
                va_end(args);
        }

        if (!test_toi_state(TOI_BOOT_TIME)) {
                printk("TuxOnIce: %s\n", local_printf_buf);
                return;
        }

        if (!can_ask) {
                continue_req = !!default_answer;
                goto post_ask;
        }

#if defined(CONFIG_VT) || defined(CONFIG_SERIAL_CONSOLE)
        console_loglevel = 7;

        say("=== TuxOnIce ===\n\n");
        if (warning_reason) {
                say("BIG FAT WARNING!! %s\n\n", local_printf_buf);
                switch (message_detail) {
                case 0:
                        say("If you continue booting, note that any image WILL"
                                "NOT BE REMOVED.\nTuxOnIce is unable to do so "
                                "because the appropriate modules aren't\n"
                                "loaded. You should manually remove the image "
                                "to avoid any\npossibility of corrupting your "
                                "filesystem(s) later.\n");
                        break;
                case 1:
                        say("If you want to use the current TuxOnIce image, "
                                "reboot and try\nagain with the same kernel "
                                "that you hibernated from. If you want\n"
                                "to forget that image, continue and the image "
                                "will be erased.\n");
                        break;
                }
                say("Press SPACE to reboot or C to continue booting with "
                        "this kernel\n\n");
                if (toi_wait > 0)
                        say("Default action if you don't select one in %d "
                                "seconds is: %s.\n",
                                toi_wait,
                                default_answer == TOI_CONTINUE_REQ ?
                                "continue booting" : "reboot");
        } else {
                say("BIG FAT WARNING!!\n\n"
                        "You have tried to resume from this image before.\n"
                        "If it failed once, it may well fail again.\n"
                        "Would you like to remove the image and boot "
                        "normally?\nThis will be equivalent to entering "
                        "noresume on the\nkernel command line.\n\n"
                        "Press SPACE to remove the image or C to continue "
                        "resuming.\n\n");
                if (toi_wait > 0)
                        say("Default action if you don't select one in %d "
                                "seconds is: %s.\n", toi_wait,
                                !!default_answer ?
                                "continue resuming" : "remove the image");
        }
        console_loglevel = orig_loglevel;

        set_toi_state(TOI_SANITY_CHECK_PROMPT);
        clear_toi_state(TOI_CONTINUE_REQ);

        if (toi_wait_for_keypress(toi_wait) == 0) /* We timed out */
                continue_req = !!default_answer;
        else
                continue_req = test_toi_state(TOI_CONTINUE_REQ);

#endif /* CONFIG_VT or CONFIG_SERIAL_CONSOLE */

post_ask:
        if ((warning_reason) && (!continue_req))
                kernel_restart(NULL);

        restore_toi_state(orig_state);
        if (continue_req)
                set_toi_state(TOI_CONTINUE_REQ);
}

#undef say

/*
 * User interface specific /sys/power/tuxonice entries.
 */

static struct toi_sysfs_data sysfs_params[] = {
#if defined(CONFIG_NET) && defined(CONFIG_SYSFS)
        SYSFS_INT("default_console_level", SYSFS_RW,
                        &toi_bkd.toi_default_console_level, 0, 7, 0, NULL),
        SYSFS_UL("debug_sections", SYSFS_RW, &toi_bkd.toi_debug_state, 0,
                        1 << 30, 0),
        SYSFS_BIT("log_everything", SYSFS_RW, &toi_bkd.toi_action, TOI_LOGALL,
                        0)
#endif
};

static struct toi_module_ops userui_ops = {
        .type                                = MISC_HIDDEN_MODULE,
        .name                                = "printk ui",
        .directory                        = "user_interface",
        .module                                = THIS_MODULE,
        .sysfs_data                        = sysfs_params,
        .num_sysfs_entries                = sizeof(sysfs_params) /
                sizeof(struct toi_sysfs_data),
};

int toi_register_ui_ops(struct ui_ops *this_ui)
{
        if (toi_current_ui) {
                printk(KERN_INFO "Only one TuxOnIce user interface module can "
                                "be loaded at a time.");
                return -EBUSY;
        }

        toi_current_ui = this_ui;

        return 0;
}

void toi_remove_ui_ops(struct ui_ops *this_ui)
{
        if (toi_current_ui != this_ui)
                return;

        toi_current_ui = NULL;
}

/* toi_console_sysfs_init
 * Description: Boot time initialisation for user interface.
 */

int toi_ui_init(void)
{
        return toi_register_module(&userui_ops);
}

void toi_ui_exit(void)
{
        toi_unregister_module(&userui_ops);
}