2 * Copyright (c) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 * Licensed under GPLv2, see file LICENSE in this source tree.
7 //config: bool "resume (3.2 kb)"
10 //config: Resume from saved "suspend-to-disk" image
12 //applet:IF_RESUME(APPLET_NOEXEC(resume, resume, BB_DIR_BIN, BB_SUID_DROP, resume))
14 //kbuild:lib-$(CONFIG_RESUME) += resume.o
18 /* This is a NOEXEC applet. Be very careful! */
20 /* name_to_dev_t() in klibc-utils supports extended device name formats,
21 * apart from the usual case where /dev/NAME already exists.
23 * - device number in hexadecimal represents itself (in dev_t layout).
24 * - device number in major:minor decimal represents itself.
25 * - if block device (or partition) with this name is found in sysfs.
26 * - if /dev/ prefix is not given, it is assumed.
28 * klibc-utils also recognizes these, but they don't work
29 * for "resume" tool purposes (thus we don't support them (yet?)):
31 * - /dev/ram (alias to /dev/ram0)
34 static dev_t name_to_dev_t(const char *devname)
36 char devfile[sizeof(int)*3 * 2 + 4];
38 unsigned major_num, minor_num;
42 if (strncmp(devname, "/dev/", 5) != 0) {
45 cptr = strchr(devname, ':');
47 /* Colon-separated decimal device number? */
49 major_num = bb_strtou(devname, NULL, 10);
51 minor_num = bb_strtou(cptr + 1, NULL, 10);
54 return makedev(major_num, minor_num);
56 /* Hexadecimal device number? */
57 dev_t res = (dev_t) bb_strtoul(devname, NULL, 16);
62 devname = xasprintf("/dev/%s", devname);
64 /* Now devname is always "/dev/FOO" */
66 if (stat(devname, &st) == 0 && S_ISBLK(st.st_mode))
69 /* Full blockdevs as well as partitions may be visible
70 * in /sys/class/block/ even if /dev is not populated.
72 sysname = xasprintf("/sys/class/block/%s/dev", devname + 5);
73 r = open_read_close(sysname, devfile, sizeof(devfile) - 1);
77 if (sscanf(devfile, "%u:%u", &major_num, &minor_num) == 2) {
78 return makedev(major_num, minor_num);
85 //usage:#define resume_trivial_usage
86 //usage: "BLOCKDEV [OFFSET]"
87 //usage:#define resume_full_usage "\n"
88 //usage: "\n""Restore system state from 'suspend-to-disk' data in BLOCKDEV"
90 int resume_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
91 int resume_main(int argc UNUSED_PARAM, char **argv)
93 unsigned long long ofs;
102 resume_device = name_to_dev_t(argv[0]);
103 if (major(resume_device) == 0) {
104 bb_error_msg_and_die("invalid resume device: %s", argv[0]);
106 ofs = (argv[1] ? xstrtoull(argv[1], 0) : 0);
108 fd = xopen("/sys/power/resume", O_WRONLY);
109 s = xasprintf("%u:%u:%llu", major(resume_device), minor(resume_device), ofs);
112 /* if write() returns, resume did not succeed */
114 return EXIT_FAILURE; /* klibc-utils exits -1 aka 255 */