mirror of
https://github.com/nitanmarcel/isodrive.git
synced 2026-03-13 04:17:24 +00:00
isodrive: remove getprop depedency, allow switching cdrom and ro modes
This commit is contained in:
12
README.md
12
README.md
@@ -10,6 +10,10 @@
|
||||
|
||||
* `sudo make install` (optional)
|
||||
|
||||
usage:
|
||||
|
||||
* `isodrive {iso} {cdrom[0/1]} {readonly[1/0]}`
|
||||
|
||||
mount
|
||||
|
||||
* `sudo isodrive /full/path/to/file.iso`
|
||||
@@ -18,5 +22,13 @@ umount
|
||||
|
||||
* `sudo isodrive`
|
||||
|
||||
mount (as cdrom)
|
||||
|
||||
* `sudo isodrive /full/path/to/file.iso 1 1`
|
||||
|
||||
mount (as read write)
|
||||
|
||||
* `sudo isodrive /full/path/to/file.iso 0 0`
|
||||
|
||||
|
||||
Inspired by https://github.com/fredldotme/ISODriveUT
|
||||
|
||||
@@ -8,12 +8,16 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
bool supported()
|
||||
{
|
||||
return fs_mount_point((char*)"configfs") != nullptr;
|
||||
}
|
||||
|
||||
char *get_gadget_root()
|
||||
{
|
||||
char *configFsRoot = fs_mount_point("configfs");
|
||||
char *usbGadgetRoot = strjin(configFsRoot, "/usb_gadget/");
|
||||
char *configFsRoot = fs_mount_point((char*)"configfs");
|
||||
char *usbGadgetRoot = strjin(configFsRoot, (char*)"/usb_gadget/");
|
||||
char *gadgetRoot = nullptr;
|
||||
char *udc = nullptr;
|
||||
|
||||
struct dirent *entry = nullptr;
|
||||
DIR *dp = nullptr;
|
||||
@@ -23,10 +27,12 @@ char *get_gadget_root()
|
||||
{
|
||||
if (entry->d_name[0] != '.')
|
||||
{
|
||||
gadgetRoot = strjin(usbGadgetRoot, entry->d_name);
|
||||
udc = sysfs_read(strjin(gadgetRoot, "/UDC"));
|
||||
if (strcmp(udc, getprop("sys.usb.controller")) == 0)
|
||||
char *gadget = strjin(usbGadgetRoot, entry->d_name);
|
||||
if (sysfs_read(strjin(gadget, (char*)"/UDC")) != nullptr)
|
||||
{
|
||||
gadgetRoot = gadget;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return gadgetRoot;
|
||||
@@ -35,7 +41,11 @@ char *get_gadget_root()
|
||||
char *get_config_root()
|
||||
{
|
||||
char *gadgetRoot = get_gadget_root();
|
||||
char *usbConfigRoot = strjin(gadgetRoot, "/configs/");
|
||||
if (gadgetRoot == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
char *usbConfigRoot = strjin(gadgetRoot, (char*)"/configs/");
|
||||
char *configRoot = nullptr;
|
||||
|
||||
struct dirent *entry = nullptr;
|
||||
@@ -53,48 +63,57 @@ char *get_config_root()
|
||||
return configRoot;
|
||||
}
|
||||
|
||||
void mount_iso(char *iso_path)
|
||||
void mount_iso(char *iso_path, char *cdrom, char *ro)
|
||||
{
|
||||
char *gadgetRoot = get_gadget_root();
|
||||
if (gadgetRoot == nullptr)
|
||||
{
|
||||
std::cerr << "No active gadget found" << std::endl;
|
||||
return;
|
||||
}
|
||||
char *configRoot = get_config_root();
|
||||
|
||||
char *functionRoot = strjin(gadgetRoot, "/functions");
|
||||
char *massStorageRoot = strjin(functionRoot, "/mass_storage.0");
|
||||
char *lunRoot = strjin(massStorageRoot, "/lun.0");
|
||||
char *udc = get_udc();
|
||||
printf("udc found %s\n", udc);
|
||||
char *functionRoot = strjin(gadgetRoot, (char*)"/functions");
|
||||
char *massStorageRoot = strjin(functionRoot, (char*)"/mass_storage.0");
|
||||
char *lunRoot = strjin(massStorageRoot, (char*)"/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");
|
||||
char *stallFile = strjin(massStorageRoot, (char*)"/stall");
|
||||
char *udcFile = strjin(gadgetRoot, (char*)"/UDC");
|
||||
char *lunFile = strjin(lunRoot, (char*)"/file");
|
||||
char *lunCdRom = strjin(lunRoot, (char*)"/cdrom");
|
||||
char *lunRo = strjin(lunRoot, (char*)"/ro");
|
||||
|
||||
reset_udc();
|
||||
set_udc((char*)"", gadgetRoot);
|
||||
|
||||
if (!isdir(massStorageRoot))
|
||||
{
|
||||
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
}
|
||||
if (!isdir(strjin(configRoot, "/mass_storage.0")))
|
||||
if (!isdir(strjin(configRoot, (char*)"/mass_storage.0")))
|
||||
{
|
||||
symlink(massStorageRoot, strjin(configRoot, "/mass_storage.0"));
|
||||
symlink(massStorageRoot, strjin(configRoot, (char*)"/mass_storage.0"));
|
||||
}
|
||||
sysfs_write(lunFile, iso_path);
|
||||
sysfs_write(lunCdRom, "0");
|
||||
sysfs_write(lunRo, "1");
|
||||
sysfs_write(lunCdRom, cdrom);
|
||||
sysfs_write(lunRo, ro);
|
||||
|
||||
set_udc();
|
||||
set_udc(udc, gadgetRoot);
|
||||
}
|
||||
|
||||
void reset_udc()
|
||||
void set_udc(char *udc, char *gadget)
|
||||
{
|
||||
char *udcFile = strjin(gadget, (char*)"/UDC");
|
||||
sysfs_write(udcFile, udc);
|
||||
}
|
||||
|
||||
char *get_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"));
|
||||
if (gadget_root == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
char *udcFile = strjin(gadget_root, (char*)"/UDC");
|
||||
return sysfs_read(udcFile);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#ifndef CONFIGFSISOMANAGER_H
|
||||
#define CONFIGFSISOMANAGER_H
|
||||
|
||||
bool supported();
|
||||
char *get_gadget_root();
|
||||
char *get_config_root();
|
||||
|
||||
void mount_iso(char *iso_path);
|
||||
void set_udc();
|
||||
void reset_udc();
|
||||
void mount_iso(char *iso_path, char *cdrom, char *ro);
|
||||
void set_udc(char *udc, char *gadget);
|
||||
char *get_udc();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,8 +6,6 @@ char *strjin(char *w1, char *w2);
|
||||
bool isdir(char *path);
|
||||
char *sysfs_read(char *path);
|
||||
void sysfs_write(char *path, char *content);
|
||||
char *getprop(char *key);
|
||||
void trim(char *s);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
22
src/main.cpp
22
src/main.cpp
@@ -5,6 +5,14 @@
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
char *iso_target = argv[1];
|
||||
char *cdrom = (char*)"0";
|
||||
char *ro = (char*)"1";
|
||||
|
||||
if (!supported())
|
||||
{
|
||||
std::cerr << "Device does not support configfs usb gadget" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (getuid() != 0)
|
||||
{
|
||||
std::cerr << "Permission denied" << std::endl;
|
||||
@@ -14,5 +22,17 @@ int main(int argc, char* argv[]){
|
||||
{
|
||||
iso_target = "";
|
||||
}
|
||||
mount_iso(iso_target);
|
||||
|
||||
if (argc > 2)
|
||||
{
|
||||
cdrom = argv[2];
|
||||
}
|
||||
if (argc > 3)
|
||||
{
|
||||
ro = argv[3];
|
||||
}
|
||||
|
||||
mount_iso(iso_target, cdrom, ro);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
32
src/util.cpp
32
src/util.cpp
@@ -11,24 +11,13 @@
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
|
||||
// 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;
|
||||
char *mount_point = nullptr;
|
||||
|
||||
mounts = setmntent("/proc/mounts", "r");
|
||||
while (NULL != (ent = getmntent(mounts))) {
|
||||
while (nullptr != (ent = getmntent(mounts))) {
|
||||
if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
|
||||
{
|
||||
mount_point = ent->mnt_dir;
|
||||
@@ -73,21 +62,12 @@ char *sysfs_read(char *path)
|
||||
}
|
||||
sysfsFile >> value;
|
||||
sysfsFile.close();
|
||||
if (value.empty())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
char *result = new char[value.length() + 1];
|
||||
strcpy(result, value.c_str());
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user