summaryrefslogtreecommitdiffstats
path: root/usr/kinit/resume
diff options
context:
space:
mode:
Diffstat (limited to 'usr/kinit/resume')
-rw-r--r--usr/kinit/resume/Kbuild34
-rw-r--r--usr/kinit/resume/resume.c25
-rw-r--r--usr/kinit/resume/resume.h7
-rw-r--r--usr/kinit/resume/resumelib.c106
4 files changed, 172 insertions, 0 deletions
diff --git a/usr/kinit/resume/Kbuild b/usr/kinit/resume/Kbuild
new file mode 100644
index 0000000..c804a85
--- /dev/null
+++ b/usr/kinit/resume/Kbuild
@@ -0,0 +1,34 @@
+#
+# Kbuild file for resume
+#
+
+static-y := static/resume
+shared-y := shared/resume
+
+# common .o files
+objs := resume.o resumelib.o
+
+# TODO - do we want a stripped version
+# TODO - do we want the static.g + shared.g directories?
+
+# Create lib.a with all object files (used by kinit)
+lib-y := $(objs)
+
+# Additional include paths files
+KLIBCCFLAGS += -I$(srctree)/$(src)/..
+
+# .o files used to built executables
+static/resume-y := $(objs)
+static/resume-lib := ../lib.a
+shared/resume-y := $(objs)
+shared/resume-lib := ../lib.a
+
+# Cleaning
+clean-dirs := static shared
+
+# install binary
+ifdef KLIBCSHAREDFLAGS
+install-y := $(shared-y)
+else
+install-y := $(static-y)
+endif
diff --git a/usr/kinit/resume/resume.c b/usr/kinit/resume/resume.c
new file mode 100644
index 0000000..2138078
--- /dev/null
+++ b/usr/kinit/resume/resume.c
@@ -0,0 +1,25 @@
+/*
+ * Handle resume from suspend-to-disk
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "resume.h"
+
+char *progname;
+
+static __noreturn usage(void)
+{
+ fprintf(stderr, "Usage: %s /dev/<resumedevice> [offset]\n", progname);
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ progname = argv[0];
+ if (argc < 2 || argc > 3)
+ usage();
+
+ return resume(argv[1], (argc > 2) ? strtoull(argv[2], NULL, 0) : 0ULL);
+}
diff --git a/usr/kinit/resume/resume.h b/usr/kinit/resume/resume.h
new file mode 100644
index 0000000..5fb929f
--- /dev/null
+++ b/usr/kinit/resume/resume.h
@@ -0,0 +1,7 @@
+#ifndef RESUME_H
+#define RESUME_H
+
+int do_resume(int argc, char *argv[]);
+int resume(const char *resume_file, unsigned long long resume_offset);
+
+#endif /* RESUME_H */
diff --git a/usr/kinit/resume/resumelib.c b/usr/kinit/resume/resumelib.c
new file mode 100644
index 0000000..03e596a
--- /dev/null
+++ b/usr/kinit/resume/resumelib.c
@@ -0,0 +1,106 @@
+/*
+ * Handle resume from suspend-to-disk
+ */
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+
+#include "kinit.h"
+#include "do_mounts.h"
+#include "resume.h"
+
+#ifndef CONFIG_PM_STD_PARTITION
+# define CONFIG_PM_STD_PARTITION ""
+#endif
+
+int do_resume(int argc, char *argv[])
+{
+ const char *resume_file = CONFIG_PM_STD_PARTITION;
+ const char *resume_arg;
+ unsigned long long resume_offset;
+
+ resume_arg = get_arg(argc, argv, "resume=");
+ resume_file = resume_arg ? resume_arg : resume_file;
+ /* No resume device specified */
+ if (!resume_file[0])
+ return 0;
+
+ resume_arg = get_arg(argc, argv, "resume_offset=");
+ resume_offset = resume_arg ? strtoull(resume_arg, NULL, 0) : 0ULL;
+
+ /* Fix: we either should consider reverting the device back to
+ ordinary swap, or (better) put that code into swapon */
+ /* Noresume requested */
+ if (get_flag(argc, argv, "noresume"))
+ return 0;
+ return resume(resume_file, resume_offset);
+}
+
+int resume(const char *resume_file, unsigned long long resume_offset)
+{
+ dev_t resume_device;
+ int attr_fd = -1;
+ char attr_value[64];
+ int len;
+
+ resume_device = name_to_dev_t(resume_file);
+
+ if (major(resume_device) == 0) {
+ fprintf(stderr, "Invalid resume device: %s\n", resume_file);
+ goto failure;
+ }
+
+ if ((attr_fd = open("/sys/power/resume_offset", O_WRONLY)) < 0)
+ goto fail_offset;
+
+ len = snprintf(attr_value, sizeof attr_value,
+ "%llu",
+ resume_offset);
+
+ /* This should never happen */
+ if (len >= sizeof attr_value)
+ goto fail_offset;
+
+ if (write(attr_fd, attr_value, len) != len)
+ goto fail_offset;
+
+ close(attr_fd);
+
+ if ((attr_fd = open("/sys/power/resume", O_WRONLY)) < 0)
+ goto fail_r;
+
+ len = snprintf(attr_value, sizeof attr_value,
+ "%u:%u",
+ major(resume_device), minor(resume_device));
+
+ /* This should never happen */
+ if (len >= sizeof attr_value)
+ goto fail_r;
+
+ dprintf("kinit: trying to resume from %s\n", resume_file);
+
+ if (write(attr_fd, attr_value, len) != len)
+ goto fail_r;
+
+ /* Okay, what are we still doing alive... */
+failure:
+ if (attr_fd >= 0)
+ close(attr_fd);
+ dprintf("kinit: No resume image, doing normal boot...\n");
+ return -1;
+
+fail_offset:
+ fprintf(stderr, "Cannot write /sys/power/resume_offset "
+ "(no software suspend kernel support, or old kernel version?)\n");
+ goto failure;
+
+fail_r:
+ fprintf(stderr, "Cannot write /sys/power/resume "
+ "(no software suspend kernel support?)\n");
+ goto failure;
+}