Move adb from multirom to trampoline, use same util.c for both, cleanup
This commit is contained in:
@@ -16,7 +16,6 @@ LOCAL_SRC_FILES:= \
|
||||
button.c \
|
||||
pong.c \
|
||||
progressdots.c \
|
||||
adb.c \
|
||||
multirom_ui_themes.c
|
||||
|
||||
|
||||
|
||||
12
multirom.c
12
multirom.c
@@ -35,13 +35,11 @@
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include "version.h"
|
||||
#include "adb.h"
|
||||
|
||||
#define REALDATA "/realdata"
|
||||
#define BUSYBOX_BIN "busybox"
|
||||
#define KEXEC_BIN "kexec"
|
||||
#define NTFS_BIN "ntfs-3g"
|
||||
#define ADBD_BIN "adbd"
|
||||
#define INTERNAL_ROM_NAME "Internal"
|
||||
#define BOOT_BLK "/dev/block/mmcblk0p2"
|
||||
#define MAX_ROM_NAME_LEN 26
|
||||
@@ -53,8 +51,7 @@
|
||||
|
||||
#define T_FOLDER 4
|
||||
|
||||
char busybox_path[64] = { 0 };
|
||||
char adbd_path[64] = { 0 };
|
||||
static char busybox_path[64] = { 0 };
|
||||
static char multirom_dir[64] = { 0 };
|
||||
static char kexec_path[64] = { 0 };
|
||||
static char ntfs_path[64] = { 0 };
|
||||
@@ -84,11 +81,9 @@ int multirom_find_base_dir(void)
|
||||
sprintf(busybox_path, "%s/%s", paths[i], BUSYBOX_BIN);
|
||||
sprintf(kexec_path, "%s/%s", paths[i], KEXEC_BIN);
|
||||
sprintf(ntfs_path, "%s/%s", paths[i], NTFS_BIN);
|
||||
sprintf(adbd_path, "%s/%s", paths[i], ADBD_BIN);
|
||||
|
||||
chmod(kexec_path, 0755);
|
||||
chmod(ntfs_path, 0755);
|
||||
chmod(adbd_path, 0755);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
@@ -108,9 +103,6 @@ int multirom(void)
|
||||
multirom_load_status(&s);
|
||||
multirom_dump_status(&s);
|
||||
|
||||
if(s.enable_adb)
|
||||
adb_init();
|
||||
|
||||
struct multirom_rom *to_boot = NULL;
|
||||
int exit = (EXIT_REBOOT | EXIT_UMOUNT);
|
||||
|
||||
@@ -170,8 +162,6 @@ int multirom(void)
|
||||
s.is_second_boot = 0;
|
||||
}
|
||||
|
||||
adb_quit();
|
||||
|
||||
multirom_save_status(&s);
|
||||
multirom_free_status(&s);
|
||||
|
||||
|
||||
@@ -101,9 +101,6 @@ struct multirom_status
|
||||
char *curr_rom_part;
|
||||
};
|
||||
|
||||
extern char busybox_path[64];
|
||||
extern char adbd_path[64];
|
||||
|
||||
int multirom(void);
|
||||
int multirom_find_base_dir(void);
|
||||
void multirom_emergency_reboot(void);
|
||||
|
||||
@@ -4,7 +4,8 @@ include $(CLEAR_VARS)
|
||||
LOCAL_SRC_FILES:= \
|
||||
trampoline.c \
|
||||
devices.c \
|
||||
util.c
|
||||
../util.c \
|
||||
adb.c
|
||||
|
||||
LOCAL_MODULE:= trampoline
|
||||
LOCAL_MODULE_TAGS := eng
|
||||
|
||||
@@ -29,14 +29,16 @@
|
||||
#include <linux/loop.h>
|
||||
|
||||
#include "adb.h"
|
||||
#include "util.h"
|
||||
#include "multirom.h"
|
||||
#include "../util.h"
|
||||
#include "log.h"
|
||||
|
||||
static pthread_t adb_thread;
|
||||
static volatile int run_thread = 0;
|
||||
static pid_t adb_pid = -1;
|
||||
|
||||
static char busybox_path[64] = { 0 };
|
||||
static char adbd_path[64] = { 0 };
|
||||
|
||||
static char * const ENV[] = {
|
||||
"PATH=/sbin:/bin:/usr/bin:/usr/sbin:/mrom_bin",
|
||||
"LD_LIBRARY_PATH=.:/sbin",
|
||||
@@ -48,8 +50,14 @@ static char * const ENV[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
static void *adb_thread_work(void *c)
|
||||
static void *adb_thread_work(void *mrom_path)
|
||||
{
|
||||
int enabled = adb_is_enabled((char*)mrom_path);
|
||||
free(mrom_path);
|
||||
|
||||
if(enabled != 0)
|
||||
return NULL;
|
||||
|
||||
adb_init_usb();
|
||||
|
||||
if(adb_init_busybox() < 0)
|
||||
@@ -57,6 +65,8 @@ static void *adb_thread_work(void *c)
|
||||
|
||||
adb_init_fs();
|
||||
|
||||
chmod(adbd_path, 0755);
|
||||
|
||||
while(run_thread)
|
||||
{
|
||||
adb_pid = fork();
|
||||
@@ -84,14 +94,17 @@ static void *adb_thread_work(void *c)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void adb_init(void)
|
||||
void adb_init(char *mrom_path)
|
||||
{
|
||||
INFO("Starting adbd\n");
|
||||
if(run_thread)
|
||||
return;
|
||||
|
||||
sprintf(busybox_path, "%s/busybox", mrom_path);
|
||||
sprintf(adbd_path, "%s/adbd", mrom_path);
|
||||
|
||||
INFO("Starting adbd\n");
|
||||
run_thread = 1;
|
||||
pthread_create(&adb_thread, NULL, adb_thread_work, NULL);
|
||||
pthread_create(&adb_thread, NULL, adb_thread_work, strdup(mrom_path));
|
||||
}
|
||||
|
||||
void adb_quit(void)
|
||||
@@ -116,20 +129,8 @@ void adb_init_usb(void)
|
||||
{
|
||||
write_file("/sys/class/android_usb/android0/enable", "0");
|
||||
|
||||
char cmdline[1024];
|
||||
char serial[64] = { 0 };
|
||||
static const char *tag = "androidboot.serialno=";
|
||||
if(multirom_get_cmdline(cmdline, sizeof(cmdline)) >= 0)
|
||||
{
|
||||
char *start = strstr(cmdline, tag);
|
||||
if(start)
|
||||
{
|
||||
start += strlen(tag);
|
||||
char *end = strchr(start, ' ');
|
||||
if(end && end-start < (int)sizeof(serial))
|
||||
strncpy(serial, start, end-start);
|
||||
}
|
||||
}
|
||||
adb_get_serial(serial, sizeof(serial));
|
||||
|
||||
write_file("/sys/class/android_usb/android0/idVendor", "18d1");
|
||||
write_file("/sys/class/android_usb/android0/idProduct", "4e42");
|
||||
@@ -187,3 +188,40 @@ void adb_cleanup(void)
|
||||
umount("/dev/pts");
|
||||
rmdir("/dev/pts");
|
||||
}
|
||||
|
||||
int adb_get_serial(char *serial, int maxlen)
|
||||
{
|
||||
FILE *f = fopen("/proc/cmdline", "r");
|
||||
if(!f)
|
||||
return -1;
|
||||
|
||||
int res = -1;
|
||||
|
||||
char cmdline[1024];
|
||||
static const char *tag = "androidboot.serialno=";
|
||||
if(fgets(cmdline, sizeof(cmdline), f))
|
||||
{
|
||||
char *start = strstr(cmdline, tag);
|
||||
if(start)
|
||||
{
|
||||
start += strlen(tag);
|
||||
char *end = strchr(start, ' ');
|
||||
if(end && end-start < maxlen)
|
||||
{
|
||||
strncpy(serial, start, end-start);
|
||||
res = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return res;
|
||||
}
|
||||
|
||||
int adb_is_enabled(char *mrom_path)
|
||||
{
|
||||
char cfg[64];
|
||||
char *cmd[] = { busybox_path, "grep", "^enable_adb=1$", cfg, NULL };
|
||||
sprintf(cfg, "%s/multirom.ini", mrom_path);
|
||||
|
||||
return run_cmd(cmd) == 0 ? 0 : -1;
|
||||
}
|
||||
@@ -18,11 +18,13 @@
|
||||
#ifndef ADB_H
|
||||
#define ADB_H
|
||||
|
||||
void adb_init(void);
|
||||
void adb_init(char *mrom_path);
|
||||
void adb_quit(void);
|
||||
void adb_init_usb(void);
|
||||
int adb_init_busybox(void);
|
||||
void adb_init_fs(void);
|
||||
void adb_cleanup(void);
|
||||
int adb_get_serial(char *serial, int maxlen);
|
||||
int adb_is_enabled(char *mrom_path);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -46,7 +46,7 @@
|
||||
#include <cutils/uevent.h>
|
||||
|
||||
#include "devices.h"
|
||||
#include "util.h"
|
||||
#include "../util.h"
|
||||
#include "log.h"
|
||||
|
||||
#define SYSFS_PREFIX "/sys"
|
||||
|
||||
@@ -26,8 +26,9 @@
|
||||
|
||||
#include "devices.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include "../util.h"
|
||||
#include "../version.h"
|
||||
#include "adb.h"
|
||||
|
||||
#define EXEC_MASK (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
|
||||
#define REALDATA "/realdata"
|
||||
@@ -86,12 +87,6 @@ static int run_multirom_bin(char *path)
|
||||
|
||||
static void run_multirom(void)
|
||||
{
|
||||
if(find_multirom() == -1)
|
||||
{
|
||||
ERROR("Could not find multirom folder!");
|
||||
return;
|
||||
}
|
||||
|
||||
char path[256];
|
||||
struct stat info;
|
||||
|
||||
@@ -206,6 +201,41 @@ exit:
|
||||
return res;
|
||||
}
|
||||
|
||||
static void mount_and_run(void)
|
||||
{
|
||||
char data_dev[128];
|
||||
|
||||
if(find_data_dev(data_dev) != 0)
|
||||
{
|
||||
ERROR("Failed to find data dev!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if(wait_for_file(data_dev, 5) < 0)
|
||||
{
|
||||
ERROR("Waiting too long for dev %s", data_dev);
|
||||
return;
|
||||
}
|
||||
|
||||
mkdir(REALDATA, 0755);
|
||||
if (mount(data_dev, REALDATA, "ext4", MS_RELATIME | MS_NOATIME,
|
||||
"user_xattr,acl,barrier=1,data=ordered,nomblk_io_submit") < 0)
|
||||
{
|
||||
ERROR("Failed to mount /realdata %d\n", errno);
|
||||
return;
|
||||
}
|
||||
|
||||
if(find_multirom() == -1)
|
||||
{
|
||||
ERROR("Could not find multirom folder!");
|
||||
return;
|
||||
}
|
||||
|
||||
adb_init(path_multirom);
|
||||
run_multirom();
|
||||
adb_quit();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
@@ -257,30 +287,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
// mount and run multirom from sdcard
|
||||
if(ok)
|
||||
{
|
||||
char data_dev[128];
|
||||
mkdir(REALDATA, 0755);
|
||||
if(find_data_dev(data_dev) != 0)
|
||||
{
|
||||
ERROR("Failed to find data dev!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(wait_for_file(data_dev, 5) < 0)
|
||||
{
|
||||
ERROR("Waiting too long for dev %s", data_dev);
|
||||
}
|
||||
else if(mount(data_dev, REALDATA, "ext4", MS_RELATIME | MS_NOATIME,
|
||||
"user_xattr,acl,barrier=1,data=ordered,discard,nomblk_io_submit") < 0)
|
||||
{
|
||||
ERROR("Failed to mount /realdata %d\n", errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
run_multirom();
|
||||
}
|
||||
}
|
||||
}
|
||||
mount_and_run();
|
||||
|
||||
// close and destroy everything
|
||||
devices_close();
|
||||
@@ -308,4 +315,4 @@ int main(int argc, char *argv[])
|
||||
int res = execve(cmd[0], cmd, NULL);
|
||||
ERROR("execve returned %d %d %s\n", res, errno, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef HAVE_SELINUX
|
||||
#include <selinux/label.h>
|
||||
#endif
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
/* for ANDROID_SOCKET_* */
|
||||
#include <cutils/sockets.h>
|
||||
|
||||
#include <private/android_filesystem_config.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
|
||||
/*
|
||||
* gettime() - returns the time in seconds of the system's monotonic clock or
|
||||
* zero on error.
|
||||
*/
|
||||
time_t gettime(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
int ret;
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
if (ret < 0) {
|
||||
ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ts.tv_sec;
|
||||
}
|
||||
|
||||
int mkdir_recursive(const char *pathname, mode_t mode)
|
||||
{
|
||||
char buf[128];
|
||||
const char *slash;
|
||||
const char *p = pathname;
|
||||
int width;
|
||||
int ret;
|
||||
struct stat info;
|
||||
|
||||
while ((slash = strchr(p, '/')) != NULL) {
|
||||
width = slash - pathname;
|
||||
p = slash + 1;
|
||||
if (width < 0)
|
||||
break;
|
||||
if (width == 0)
|
||||
continue;
|
||||
if ((unsigned int)width > sizeof(buf) - 1) {
|
||||
ERROR("path too long for mkdir_recursive\n");
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, pathname, width);
|
||||
buf[width] = 0;
|
||||
if (stat(buf, &info) != 0) {
|
||||
ret = mkdir(buf, mode);
|
||||
if (ret && errno != EEXIST)
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
ret = mkdir(pathname, mode);
|
||||
if (ret && errno != EEXIST)
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sanitize(char *s)
|
||||
{
|
||||
if (!s)
|
||||
return;
|
||||
while (isalnum(*s))
|
||||
s++;
|
||||
*s = 0;
|
||||
}
|
||||
void make_link(const char *oldpath, const char *newpath)
|
||||
{
|
||||
int ret;
|
||||
char buf[256];
|
||||
char *slash;
|
||||
int width;
|
||||
|
||||
slash = strrchr(newpath, '/');
|
||||
if (!slash)
|
||||
return;
|
||||
width = slash - newpath;
|
||||
if (width <= 0 || width > (int)sizeof(buf) - 1)
|
||||
return;
|
||||
memcpy(buf, newpath, width);
|
||||
buf[width] = 0;
|
||||
ret = mkdir_recursive(buf, 0755);
|
||||
if (ret)
|
||||
ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
|
||||
|
||||
ret = symlink(oldpath, newpath);
|
||||
if (ret && errno != EEXIST)
|
||||
ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
|
||||
}
|
||||
|
||||
void remove_link(const char *oldpath, const char *newpath)
|
||||
{
|
||||
char path[256];
|
||||
ssize_t ret;
|
||||
ret = readlink(newpath, path, sizeof(path) - 1);
|
||||
if (ret <= 0)
|
||||
return;
|
||||
path[ret] = 0;
|
||||
if (!strcmp(path, oldpath))
|
||||
unlink(newpath);
|
||||
}
|
||||
|
||||
int wait_for_file(const char *filename, int timeout)
|
||||
{
|
||||
struct stat info;
|
||||
time_t timeout_time = gettime() + timeout;
|
||||
int ret = -1;
|
||||
|
||||
while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
|
||||
usleep(10000);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
time_t gettime(void);
|
||||
int mkdir_recursive(const char *pathname, mode_t mode);
|
||||
void sanitize(char *p);
|
||||
void make_link(const char *oldpath, const char *newpath);
|
||||
void remove_link(const char *oldpath, const char *newpath);
|
||||
int wait_for_file(const char *filename, int timeout);
|
||||
|
||||
#endif
|
||||
316
util.c
316
util.c
@@ -34,14 +34,29 @@
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
/* for ANDROID_SOCKET_* */
|
||||
#include <cutils/sockets.h>
|
||||
|
||||
#include <private/android_filesystem_config.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
/*
|
||||
* gettime() - returns the time in seconds of the system's monotonic clock or
|
||||
* zero on error.
|
||||
*/
|
||||
time_t gettime(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
int ret;
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
if (ret < 0) {
|
||||
ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ts.tv_sec;
|
||||
}
|
||||
|
||||
/*
|
||||
* android_name_to_id - returns the integer uid/gid associated with the given
|
||||
* name, or -1U on error.
|
||||
@@ -80,206 +95,6 @@ unsigned int decode_uid(const char *s)
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
* create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
|
||||
* ("/dev/socket") as dictated in init.rc. This socket is inherited by the
|
||||
* daemon. We communicate the file descriptor's value via the environment
|
||||
* variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
|
||||
*/
|
||||
int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid)
|
||||
{
|
||||
struct sockaddr_un addr;
|
||||
int fd, ret;
|
||||
#ifdef HAVE_SELINUX
|
||||
char *secon;
|
||||
#endif
|
||||
|
||||
fd = socket(PF_UNIX, type, 0);
|
||||
if (fd < 0) {
|
||||
ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&addr, 0 , sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
|
||||
name);
|
||||
|
||||
ret = unlink(addr.sun_path);
|
||||
if (ret != 0 && errno != ENOENT) {
|
||||
ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
|
||||
goto out_close;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SELINUX
|
||||
secon = NULL;
|
||||
if (sehandle) {
|
||||
ret = selabel_lookup(sehandle, &secon, addr.sun_path, S_IFSOCK);
|
||||
if (ret == 0)
|
||||
setfscreatecon(secon);
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
|
||||
if (ret) {
|
||||
ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
|
||||
goto out_unlink;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SELINUX
|
||||
setfscreatecon(NULL);
|
||||
freecon(secon);
|
||||
#endif
|
||||
|
||||
chown(addr.sun_path, uid, gid);
|
||||
chmod(addr.sun_path, perm);
|
||||
|
||||
INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
|
||||
addr.sun_path, perm, uid, gid);
|
||||
|
||||
return fd;
|
||||
|
||||
out_unlink:
|
||||
unlink(addr.sun_path);
|
||||
out_close:
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* reads a file, making sure it is terminated with \n \0 */
|
||||
void *read_file(const char *fn, unsigned *_sz)
|
||||
{
|
||||
char *data;
|
||||
int sz;
|
||||
int fd;
|
||||
struct stat sb;
|
||||
|
||||
data = 0;
|
||||
fd = open(fn, O_RDONLY);
|
||||
if(fd < 0) return 0;
|
||||
|
||||
// for security reasons, disallow world-writable
|
||||
// or group-writable files
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
ERROR("fstat failed for '%s'\n", fn);
|
||||
goto oops;
|
||||
}
|
||||
if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
|
||||
ERROR("skipping insecure file '%s'\n", fn);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
sz = lseek(fd, 0, SEEK_END);
|
||||
if(sz < 0) goto oops;
|
||||
|
||||
if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
|
||||
|
||||
data = (char*) malloc(sz + 2);
|
||||
if(data == 0) goto oops;
|
||||
|
||||
if(read(fd, data, sz) != sz) goto oops;
|
||||
close(fd);
|
||||
data[sz] = '\n';
|
||||
data[sz+1] = 0;
|
||||
if(_sz) *_sz = sz;
|
||||
return data;
|
||||
|
||||
oops:
|
||||
close(fd);
|
||||
if(data != 0) free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define MAX_MTD_PARTITIONS 16
|
||||
|
||||
static struct {
|
||||
char name[16];
|
||||
int number;
|
||||
} mtd_part_map[MAX_MTD_PARTITIONS];
|
||||
|
||||
static int mtd_part_count = -1;
|
||||
|
||||
static void find_mtd_partitions(void)
|
||||
{
|
||||
int fd;
|
||||
char buf[1024];
|
||||
char *pmtdbufp;
|
||||
ssize_t pmtdsize;
|
||||
int r;
|
||||
|
||||
fd = open("/proc/mtd", O_RDONLY);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
pmtdsize = read(fd, buf, sizeof(buf) - 1);
|
||||
pmtdbufp = buf;
|
||||
while (pmtdsize > 0) {
|
||||
int mtdnum, mtdsize, mtderasesize;
|
||||
char mtdname[16];
|
||||
mtdname[0] = '\0';
|
||||
mtdnum = -1;
|
||||
r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
|
||||
&mtdnum, &mtdsize, &mtderasesize, mtdname);
|
||||
if ((r == 4) && (mtdname[0] == '"')) {
|
||||
char *x = strchr(mtdname + 1, '"');
|
||||
if (x) {
|
||||
*x = 0;
|
||||
}
|
||||
INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
|
||||
if (mtd_part_count < MAX_MTD_PARTITIONS) {
|
||||
strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
|
||||
mtd_part_map[mtd_part_count].number = mtdnum;
|
||||
mtd_part_count++;
|
||||
} else {
|
||||
ERROR("too many mtd partitions\n");
|
||||
}
|
||||
}
|
||||
while (pmtdsize > 0 && *pmtdbufp != '\n') {
|
||||
pmtdbufp++;
|
||||
pmtdsize--;
|
||||
}
|
||||
if (pmtdsize > 0) {
|
||||
pmtdbufp++;
|
||||
pmtdsize--;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int mtd_name_to_number(const char *name)
|
||||
{
|
||||
int n;
|
||||
if (mtd_part_count < 0) {
|
||||
mtd_part_count = 0;
|
||||
find_mtd_partitions();
|
||||
}
|
||||
for (n = 0; n < mtd_part_count; n++) {
|
||||
if (!strcmp(name, mtd_part_map[n].name)) {
|
||||
return mtd_part_map[n].number;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* gettime() - returns the time in seconds of the system's monotonic clock or
|
||||
* zero on error.
|
||||
*/
|
||||
time_t gettime(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
int ret;
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
if (ret < 0) {
|
||||
ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ts.tv_sec;
|
||||
}
|
||||
|
||||
int mkdir_recursive(const char *pathname, mode_t mode)
|
||||
{
|
||||
char buf[128];
|
||||
@@ -380,101 +195,6 @@ int wait_for_file(const char *filename, int timeout)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void open_devnull_stdio(void)
|
||||
{
|
||||
int fd;
|
||||
static const char *name = "/dev/__null__";
|
||||
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
|
||||
fd = open(name, O_RDWR);
|
||||
unlink(name);
|
||||
if (fd >= 0) {
|
||||
dup2(fd, 0);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
if (fd > 2) {
|
||||
close(fd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void get_hardware_name(char *hardware, unsigned int *revision)
|
||||
{
|
||||
char data[1024];
|
||||
int fd, n;
|
||||
char *x, *hw, *rev;
|
||||
|
||||
/* Hardware string was provided on kernel command line */
|
||||
if (hardware[0])
|
||||
return;
|
||||
|
||||
fd = open("/proc/cpuinfo", O_RDONLY);
|
||||
if (fd < 0) return;
|
||||
|
||||
n = read(fd, data, 1023);
|
||||
close(fd);
|
||||
if (n < 0) return;
|
||||
|
||||
data[n] = 0;
|
||||
hw = strstr(data, "\nHardware");
|
||||
rev = strstr(data, "\nRevision");
|
||||
|
||||
if (hw) {
|
||||
x = strstr(hw, ": ");
|
||||
if (x) {
|
||||
x += 2;
|
||||
n = 0;
|
||||
while (*x && *x != '\n') {
|
||||
if (!isspace(*x))
|
||||
hardware[n++] = tolower(*x);
|
||||
x++;
|
||||
if (n == 31) break;
|
||||
}
|
||||
hardware[n] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (rev) {
|
||||
x = strstr(rev, ": ");
|
||||
if (x) {
|
||||
*revision = strtoul(x + 2, 0, 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void import_kernel_cmdline(int in_qemu,
|
||||
void (*import_kernel_nv)(char *name, int in_qemu))
|
||||
{
|
||||
char cmdline[1024];
|
||||
char *ptr;
|
||||
int fd;
|
||||
|
||||
fd = open("/proc/cmdline", O_RDONLY);
|
||||
if (fd >= 0) {
|
||||
int n = read(fd, cmdline, 1023);
|
||||
if (n < 0) n = 0;
|
||||
|
||||
/* get rid of trailing newline, it happens */
|
||||
if (n > 0 && cmdline[n-1] == '\n') n--;
|
||||
|
||||
cmdline[n] = 0;
|
||||
close(fd);
|
||||
} else {
|
||||
cmdline[0] = 0;
|
||||
}
|
||||
|
||||
ptr = cmdline;
|
||||
while (ptr && *ptr) {
|
||||
char *x = strchr(ptr, ' ');
|
||||
if (x != 0) *x++ = 0;
|
||||
import_kernel_nv(ptr, in_qemu);
|
||||
ptr = x;
|
||||
}
|
||||
}
|
||||
|
||||
int copy_file(const char *from, const char *to)
|
||||
{
|
||||
FILE *in = fopen(from, "r");
|
||||
|
||||
8
util.h
8
util.h
@@ -26,21 +26,13 @@
|
||||
|
||||
static const char *coldboot_done = "/dev/.coldboot_done";
|
||||
|
||||
int mtd_name_to_number(const char *name);
|
||||
int create_socket(const char *name, int type, mode_t perm,
|
||||
uid_t uid, gid_t gid);
|
||||
void *read_file(const char *fn, unsigned *_sz);
|
||||
time_t gettime(void);
|
||||
unsigned int decode_uid(const char *s);
|
||||
|
||||
int mkdir_recursive(const char *pathname, mode_t mode);
|
||||
void sanitize(char *p);
|
||||
int make_link(const char *oldpath, const char *newpath);
|
||||
void remove_link(const char *oldpath, const char *newpath);
|
||||
int wait_for_file(const char *filename, int timeout);
|
||||
void open_devnull_stdio(void);
|
||||
void get_hardware_name(char *hardware, unsigned int *revision);
|
||||
void import_kernel_cmdline(int in_qemu, void (*import_kernel_nv)(char *name, int in_qemu));
|
||||
int copy_file(const char *from, const char *to);
|
||||
int mkdir_with_perms(const char *path, mode_t mode, const char *owner, const char *group);
|
||||
int write_file(const char *path, const char *value);
|
||||
|
||||
Reference in New Issue
Block a user