Mount USB storage
[platform/core/system/system-recovery.git] / src / system-recovery / recovery-main.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _POSIX_SOURCE
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <stdbool.h>
27 #include <libconfig.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/reboot.h>
35
36 #include <asm-generic/setup.h> // for COMMAND_LINE_SIZE
37
38 #include "system-recovery.h"
39
40 void sys_power_reboot(void)
41 {
42         reboot(RB_AUTOBOOT);
43 }
44
45 #define KERNEL_CMDLINE_KEY "tizen.recovery"
46 #define ACTION_BUFFER_SIZE 256
47 #define _STR(x) #x
48 #define STR(x) _STR(x)
49
50 // looks for tizen.recovery= key in kernel command line
51 char *get_action_from_cmdline(void)
52 {
53         FILE *fp;
54         char cmdline[COMMAND_LINE_SIZE];
55         int len;
56
57         fp = fopen("/proc/cmdline", "r");
58         if (!fp)
59                 return NULL;
60
61         char *p = fgets(cmdline, sizeof cmdline, fp);
62         fclose(fp);
63         if (!p)
64                 return NULL;
65
66         const char *prefix = KERNEL_CMDLINE_KEY "=";
67         p = strstr(cmdline, prefix);
68         if (!p)
69                 return NULL;
70         p += strlen(prefix);
71
72         for (len = 0; *(p + len) != 0 && !isspace(*(p + len)); ++len)
73                 ; /* skip */
74
75         return strndup(p, len);
76 }
77
78 char *get_action_from_file(void)
79 {
80         FILE *fp;
81         char buf[ACTION_BUFFER_SIZE];
82         int ret;
83
84         fp = fopen(SYSTEM_RECOVERY_ACTION_FILE, "r");
85         if (!fp) {
86                 LOGD("Could not open recovery action file\n");
87                 return NULL;
88         }
89
90         ret = fscanf(fp, "%" STR(ACTION_BUFFER_SIZE) "s", buf);
91         fclose(fp);
92         if (unlink(SYSTEM_RECOVERY_ACTION_FILE) < 0)
93                 LOGD("Could not unlink recovery action file: %m\n");
94
95         if (ret != 1)
96                 return NULL;
97
98         return strndup(buf, ACTION_BUFFER_SIZE);
99 }
100
101 int main(void)
102 {
103         config_t cfg;
104         int ret;
105
106         LOGD("[main] recovery started.\n");
107
108         config_init(&cfg);
109         ret = config_read_file(&cfg, SYSTEM_RECOVERY_CONFIG_FILE);
110         if (ret == CONFIG_FALSE) {
111                 LOGD("Can't read config file");
112                 return 1;
113         }
114
115 #ifdef RECOVERY_GUI
116         ret = recovery_gui(&cfg);
117 #else
118         ret = recovery_headless(&cfg);
119 #endif
120
121         LOGD("[main] recovery finished.\n");
122         config_destroy(&cfg);
123
124         return ret;
125 }