misc: add Tizen reboot notifier for passing reboot parameter
[platform/kernel/linux-rpi.git] / drivers / misc / tizen-inform-reboot.c
1 /*
2  * Tizen reboot parameter passing notifier
3  *
4  * Written by: Junghoon Kim <jhoon20.kim@samsung.com>
5  *
6  * Copyright (C) 2017 Samsung Electronics Co., Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/syscalls.h>
16 #include <linux/file.h>
17 #include <linux/fcntl.h>
18 #include <linux/uaccess.h>
19
20 static int inform_reboot_notifier(struct notifier_block *nb,
21                                                 unsigned long val, void *buf)
22 {
23         char *cmd = buf;
24         char *filename = CONFIG_TIZEN_INFORM_PATH;
25         struct file *file;
26         int fd;
27         loff_t pos = 0;
28         mm_segment_t old_fs = get_fs();
29
30         set_fs(KERNEL_DS);
31
32         fd = sys_open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
33         if (fd >= 0) {
34                 file = fget(fd);
35                 if (file) {
36                         if (cmd) {
37                                 if (!strncmp(cmd, "fota", 4))
38                                         cmd = "upgr";
39                                 else if (!strncmp(cmd, "recovery", 8))
40                                         cmd = "rcvr";
41                                 else
42                                         cmd = "ndef";
43                         } else
44                                 cmd = "norm";
45
46                         vfs_write(file, cmd, strlen(cmd), &pos);
47                         vfs_fsync(file, 0);
48                         fput(file);
49                 }
50                 sys_close(fd);
51                 /* to guarantee all file data and metadata sync */
52                 emergency_sync();
53         } else {
54                 pr_err("Reboot parameter passing is failed.\n"
55                                 "Inform file path should be described correctly in config.\n");
56         }
57
58         set_fs(old_fs);
59
60         return NOTIFY_DONE;
61 }
62
63 static struct notifier_block nb_inform_reboot_block = {
64         .notifier_call = inform_reboot_notifier
65 };
66
67 static int __init inform_reboot_init(void)
68 {
69         /* to support reboot parameter passing */
70         register_reboot_notifier(&nb_inform_reboot_block);
71         return 0;
72 }
73
74 subsys_initcall(inform_reboot_init);