isodrive: first commit

This commit is contained in:
Marcel Alexandru Nitan
2023-01-31 20:36:12 +02:00
commit f55ca622aa
7 changed files with 916 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
#include "configfsisomanager.h"
#include <dirent.h>
#include <unistd.h>
char *get_gadget_root()
{
char *configFsRoot = fs_mount_point("configfs");
char *usbGadgetRoot = strjin(configFsRoot, "/usb_gadget/");
char *gadgetRoot = nullptr;
struct dirent *entry = nullptr;
DIR *dp = nullptr;
dp = opendir(usbGadgetRoot);
if (dp != nullptr)
while ((entry = readdir(dp)))
{
if (entry->d_name[0] != '.')
{
gadgetRoot = strjin(usbGadgetRoot, entry->d_name);
break;
}
}
return gadgetRoot;
}
char *get_config_root()
{
char *gadgetRoot = get_gadget_root();
char *usbConfigRoot = strjin(gadgetRoot, "/configs/");
char *configRoot = nullptr;
struct dirent *entry = nullptr;
DIR *dp = nullptr;
dp = opendir(usbConfigRoot);
if (dp != nullptr)
while ((entry = readdir(dp)))
{
if (entry->d_name[0] != '.')
{
configRoot = strjin(usbConfigRoot, entry->d_name);
break;
}
}
return configRoot;
}
void mount_iso(char *iso_path)
{
char *gadgetRoot = get_gadget_root();
char *configRoot = get_config_root();
char *functionRoot = strjin(gadgetRoot, "/functions");
char *massStorageRoot = strjin(functionRoot, "/mass_storage.0");
char *lunRoot = strjin(massStorageRoot, "/lun.0");
char *stallFile = strjin(massStorageRoot, "stall");
char *udcFile = strjin(gadgetRoot, "/UDC");
char *lunFile = strjin(lunRoot, "/file");
char *lunCdRom = strjin(lunRoot, "/cdrom");
char *lunRo = strjin(lunRoot, "/ro");
reset_udc();
if (!isdir(massStorageRoot))
{
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
if (!isdir(strjin(configRoot, "/mass_storage.0")))
{
symlink(massStorageRoot, strjin(configRoot, "/mass_storage.0"));
}
sysfs_write(lunFile, iso_path);
sysfs_write(lunCdRom, "1");
sysfs_write(lunRo, "1");
set_udc();
}
void reset_udc()
{
char *gadget_root = get_gadget_root();
char *udcFile = strjin(gadget_root, "/UDC");
sysfs_write(udcFile, "");
}
void set_udc()
{
char *gadget_root = get_gadget_root();
char *udcFile = strjin(gadget_root, "/UDC");
sysfs_write(udcFile, getprop("sys.usb.controller"));
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef CONFIGFSISOMANAGER_H
#define CONFIGFSISOMANAGER_H
char *get_gadget_root();
char *get_config_root();
void mount_iso(char *iso_path);
void set_udc();
void reset_udc();
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef UTIL_H
#define UTIL_H
char *fs_mount_point(char *filesystem_type);
char *strjin(char *w1, char *w2);
bool isdir(char *path);
void sysfs_write(char *path, char *content);
char *getprop(char *key);
void trim(char *s);
#endif
+24
View File
@@ -0,0 +1,24 @@
#include "configfsisomanager.h"
#include <iostream>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[]){
char *iso_target = argv[1];
if (getuid() != 0)
{
std::cerr << "Permission denied" << std::endl;
return 1;
}
if (argc == 1)
{
std::cerr << "Not enough args" << std::endl;
return 1;
}
if (iso_target[0] != '/')
{
std::cerr << "Iso path is not absolute" << std::endl;
return 1;
}
mount_iso(iso_target);
}
+81
View File
@@ -0,0 +1,81 @@
#include <fcntl.h>
#include <mntent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <memory>
// https://stackoverflow.com/a/123724
void trim(char *s)
{
char * p = s;
int l = strlen(p);
while(isspace(p[l - 1])) p[--l] = 0;
while(* p && isspace(* p)) ++p, --l;
memmove(s, p, l + 1);
}
char *fs_mount_point(char *filesystem_type) {
struct mntent *ent;
FILE *mounts;
char *mount_point = NULL;
mounts = setmntent("/proc/mounts", "r");
while (NULL != (ent = getmntent(mounts))) {
if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
{
mount_point = ent->mnt_dir;
break;
}
}
return mount_point;
}
char *strjin(char *w1, char *w2)
{
char *s = new char[strlen(w1)+strlen(w2)+1];
strcpy(s, w1);
strcat(s, w2);
return s;
}
bool isdir(char *path)
{
struct stat sb;
if (stat(path, &sb) == 0)
return true;
else
return false;
}
void sysfs_write(char *path, char *content)
{
std::cout << "Write: " << content << " -> " << path << std::endl;
int fd;
fd = open(path, O_WRONLY);
write(fd, content, 1);
close(fd);
}
char *getprop(char *key)
{
std::array<char, 128> buffer;
char *value;
char *cmd = strjin("getprop ", key);
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
value = strjin("", buffer.data());
trim(value);
return value;
}