^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Disk protection for HP/DELL machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2008 Eric Piel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2009 Pavel Machek <pavel@ucw.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2012 Sonal Santan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright 2014 Pali Rohár <pali@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <syslog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int noled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static char unload_heads_path[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static char device_path[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static const char app_name[] = "FREE FALL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int set_unload_heads_path(char *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) strncpy(device_path, device, sizeof(device_path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) "/sys/block/%s/device/unload_heads", device+5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int valid_disk(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int fd = open(unload_heads_path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) perror(unload_heads_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void write_int(char *path, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) char buf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int fd = open(path, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) sprintf(buf, "%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (write(fd, buf, strlen(buf)) != strlen(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static void set_led(int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (noled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) write_int("/sys/class/leds/hp::hddprotect/brightness", on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void protect(int seconds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const char *str = (seconds == 0) ? "Unparked" : "Parked";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) write_int(unload_heads_path, seconds*1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) syslog(LOG_INFO, "%s %s disk head\n", str, device_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int on_ac(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* /sys/class/power_supply/AC0/online */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int lid_open(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* /proc/acpi/button/lid/LID/state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void ignore_me(int signum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) protect(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) set_led(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int fd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct sched_param param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (argc == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = set_unload_heads_path("/dev/sda");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) else if (argc == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ret = set_unload_heads_path(argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ret || !valid_disk()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) fd = open("/dev/freefall", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) perror("/dev/freefall");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (stat("/sys/class/leds/hp::hddprotect/brightness", &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) noled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (daemon(0, 0) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) perror("daemon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) openlog(app_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) param.sched_priority = sched_get_priority_max(SCHED_FIFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) sched_setscheduler(0, SCHED_FIFO, ¶m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mlockall(MCL_CURRENT|MCL_FUTURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) signal(SIGALRM, ignore_me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = read(fd, &count, sizeof(count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) alarm(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if ((ret == -1) && (errno == EINTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Alarm expired, time to unpark the heads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (ret != sizeof(count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) protect(21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) set_led(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (1 || on_ac() || lid_open())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) alarm(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) alarm(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) closelog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }