2 * Copyright (C) 2016 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, visit the http://fsf.org website.
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
31 #define PREFIX "/sys/class/android_usb/android0/"
33 int do_write(char *fname, char *text)
36 fd = open(fname, O_WRONLY);
37 write(fd, text, strlen(text));
42 int do_read(char *fname, char *buffer, size_t len)
46 fd = open(fname, O_RDONLY);
48 fprintf(stderr, "failed to open %s: %s\n", fname,
52 r = read(fd, buffer, len);
58 do_mknod(char *path, char *name, int block, int wait)
65 len = strlen(path) + strlen(name) + 6;
66 fullpath = malloc(len);
68 fprintf(stderr, "failed to allocate buffer\n");
71 snprintf(fullpath, len, "%s/%s/dev", path, name);
73 r = do_read(fullpath, dev, sizeof(dev));
75 fprintf(stderr, "failed to read %s: %s\n", fullpath,
79 } while (r < 0 && wait);
80 r = sscanf(dev, "%d:%d", &maj, &min);
82 fprintf(stderr, "failed to read devno from %s\n", dev);
85 snprintf(fullpath, len, "/dev/%s", name);
86 r = mknod(fullpath, 0666 | (block ? S_IFBLK : S_IFCHR), makedev(maj, min));
88 fprintf(stderr, "failed to mknod %s: %s\n", fullpath,
96 int main(int argc, char **argv)
101 char *args[] = { "bash", "-l", NULL };
102 r = devmount_setup();
104 fprintf(stderr, "failed to mount devtmpfs, proceeding anyway\n");
106 r = sysfsmount_setup();
108 fprintf(stderr, "failed to mount sysfs, proceeding anyway\n");
110 do_write(PREFIX "/enable", "0");
111 do_write(PREFIX "/idVendor", "04E8");
112 do_write(PREFIX "/idProduct", "6860");
113 do_write(PREFIX "/f_acm/acm_transports", "tty");
114 do_write(PREFIX "/functions", "acm");
115 do_write(PREFIX "/enable", "1");
116 do_mknod("/sys/class/tty", "ttyGS0", 0, 1);
117 fd = open("/dev/ttyGS0", O_RDWR);
118 for (i = 0; i < 10; i++) {
119 write(fd, "Hello World!\n", 13);
127 r = do_mknod("/sys/block/mmcblk1", "mmcblk1p3", 1, 1);
129 fprintf(stderr, "Failed to create mmcblk1p3\n");
132 printf("Mounting root\n");
133 r = mkdir("/root", 0755);
134 if (r) printf("failed mkdir\n");
135 r = mount("/dev/mmcblk1p3", "/root", "ext4", MS_NOATIME, NULL);
136 if (r) printf("failed mount: %d\n", errno);
137 printf("Chroot and chdir\n");
139 if (r) printf("failed first chdir\n");
141 if (r) printf("failed chroot\n");
143 if (r) printf("failed second chdir\n");
144 execve("/bin/bash", args, NULL);