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:
@@ -10,6 +10,10 @@
|
|||||||
|
|
||||||
* `sudo make install` (optional)
|
* `sudo make install` (optional)
|
||||||
|
|
||||||
|
usage:
|
||||||
|
|
||||||
|
* `isodrive {iso} {cdrom[0/1]} {readonly[1/0]}`
|
||||||
|
|
||||||
mount
|
mount
|
||||||
|
|
||||||
* `sudo isodrive /full/path/to/file.iso`
|
* `sudo isodrive /full/path/to/file.iso`
|
||||||
@@ -18,5 +22,13 @@ umount
|
|||||||
|
|
||||||
* `sudo isodrive`
|
* `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
|
Inspired by https://github.com/fredldotme/ISODriveUT
|
||||||
|
|||||||
+52
-33
@@ -8,12 +8,16 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
bool supported()
|
||||||
|
{
|
||||||
|
return fs_mount_point((char*)"configfs") != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
char *get_gadget_root()
|
char *get_gadget_root()
|
||||||
{
|
{
|
||||||
char *configFsRoot = fs_mount_point("configfs");
|
char *configFsRoot = fs_mount_point((char*)"configfs");
|
||||||
char *usbGadgetRoot = strjin(configFsRoot, "/usb_gadget/");
|
char *usbGadgetRoot = strjin(configFsRoot, (char*)"/usb_gadget/");
|
||||||
char *gadgetRoot = nullptr;
|
char *gadgetRoot = nullptr;
|
||||||
char *udc = nullptr;
|
|
||||||
|
|
||||||
struct dirent *entry = nullptr;
|
struct dirent *entry = nullptr;
|
||||||
DIR *dp = nullptr;
|
DIR *dp = nullptr;
|
||||||
@@ -23,10 +27,12 @@ char *get_gadget_root()
|
|||||||
{
|
{
|
||||||
if (entry->d_name[0] != '.')
|
if (entry->d_name[0] != '.')
|
||||||
{
|
{
|
||||||
gadgetRoot = strjin(usbGadgetRoot, entry->d_name);
|
char *gadget = strjin(usbGadgetRoot, entry->d_name);
|
||||||
udc = sysfs_read(strjin(gadgetRoot, "/UDC"));
|
if (sysfs_read(strjin(gadget, (char*)"/UDC")) != nullptr)
|
||||||
if (strcmp(udc, getprop("sys.usb.controller")) == 0)
|
{
|
||||||
|
gadgetRoot = gadget;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return gadgetRoot;
|
return gadgetRoot;
|
||||||
@@ -35,7 +41,11 @@ char *get_gadget_root()
|
|||||||
char *get_config_root()
|
char *get_config_root()
|
||||||
{
|
{
|
||||||
char *gadgetRoot = get_gadget_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;
|
char *configRoot = nullptr;
|
||||||
|
|
||||||
struct dirent *entry = nullptr;
|
struct dirent *entry = nullptr;
|
||||||
@@ -53,48 +63,57 @@ char *get_config_root()
|
|||||||
return configRoot;
|
return configRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mount_iso(char *iso_path)
|
void mount_iso(char *iso_path, char *cdrom, char *ro)
|
||||||
{
|
{
|
||||||
char *gadgetRoot = get_gadget_root();
|
char *gadgetRoot = get_gadget_root();
|
||||||
|
if (gadgetRoot == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr << "No active gadget found" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
char *configRoot = get_config_root();
|
char *configRoot = get_config_root();
|
||||||
|
char *udc = get_udc();
|
||||||
char *functionRoot = strjin(gadgetRoot, "/functions");
|
printf("udc found %s\n", udc);
|
||||||
char *massStorageRoot = strjin(functionRoot, "/mass_storage.0");
|
char *functionRoot = strjin(gadgetRoot, (char*)"/functions");
|
||||||
char *lunRoot = strjin(massStorageRoot, "/lun.0");
|
char *massStorageRoot = strjin(functionRoot, (char*)"/mass_storage.0");
|
||||||
|
char *lunRoot = strjin(massStorageRoot, (char*)"/lun.0");
|
||||||
|
|
||||||
char *stallFile = strjin(massStorageRoot, "/stall");
|
char *stallFile = strjin(massStorageRoot, (char*)"/stall");
|
||||||
char *udcFile = strjin(gadgetRoot, "/UDC");
|
char *udcFile = strjin(gadgetRoot, (char*)"/UDC");
|
||||||
char *lunFile = strjin(lunRoot, "/file");
|
char *lunFile = strjin(lunRoot, (char*)"/file");
|
||||||
char *lunCdRom = strjin(lunRoot, "/cdrom");
|
char *lunCdRom = strjin(lunRoot, (char*)"/cdrom");
|
||||||
char *lunRo = strjin(lunRoot, "/ro");
|
char *lunRo = strjin(lunRoot, (char*)"/ro");
|
||||||
|
|
||||||
reset_udc();
|
set_udc((char*)"", gadgetRoot);
|
||||||
|
|
||||||
if (!isdir(massStorageRoot))
|
if (!isdir(massStorageRoot))
|
||||||
{
|
{
|
||||||
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
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(lunFile, iso_path);
|
||||||
sysfs_write(lunCdRom, "0");
|
sysfs_write(lunCdRom, cdrom);
|
||||||
sysfs_write(lunRo, "1");
|
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 *gadget_root = get_gadget_root();
|
||||||
char *udcFile = strjin(gadget_root, "/UDC");
|
if (gadget_root == nullptr)
|
||||||
sysfs_write(udcFile, "");
|
{
|
||||||
}
|
return nullptr;
|
||||||
|
}
|
||||||
void set_udc()
|
char *udcFile = strjin(gadget_root, (char*)"/UDC");
|
||||||
{
|
return sysfs_read(udcFile);
|
||||||
char *gadget_root = get_gadget_root();
|
|
||||||
char *udcFile = strjin(gadget_root, "/UDC");
|
|
||||||
sysfs_write(udcFile, getprop("sys.usb.controller"));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#ifndef CONFIGFSISOMANAGER_H
|
#ifndef CONFIGFSISOMANAGER_H
|
||||||
#define CONFIGFSISOMANAGER_H
|
#define CONFIGFSISOMANAGER_H
|
||||||
|
|
||||||
|
bool supported();
|
||||||
char *get_gadget_root();
|
char *get_gadget_root();
|
||||||
char *get_config_root();
|
char *get_config_root();
|
||||||
|
|
||||||
void mount_iso(char *iso_path);
|
void mount_iso(char *iso_path, char *cdrom, char *ro);
|
||||||
void set_udc();
|
void set_udc(char *udc, char *gadget);
|
||||||
void reset_udc();
|
char *get_udc();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ char *strjin(char *w1, char *w2);
|
|||||||
bool isdir(char *path);
|
bool isdir(char *path);
|
||||||
char *sysfs_read(char *path);
|
char *sysfs_read(char *path);
|
||||||
void sysfs_write(char *path, char *content);
|
void sysfs_write(char *path, char *content);
|
||||||
char *getprop(char *key);
|
|
||||||
void trim(char *s);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
+21
-1
@@ -5,6 +5,14 @@
|
|||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
char *iso_target = argv[1];
|
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)
|
if (getuid() != 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Permission denied" << std::endl;
|
std::cerr << "Permission denied" << std::endl;
|
||||||
@@ -14,5 +22,17 @@ int main(int argc, char* argv[]){
|
|||||||
{
|
{
|
||||||
iso_target = "";
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-26
@@ -11,24 +11,13 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdio>
|
#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) {
|
char *fs_mount_point(char *filesystem_type) {
|
||||||
struct mntent *ent;
|
struct mntent *ent;
|
||||||
FILE *mounts;
|
FILE *mounts;
|
||||||
char *mount_point = NULL;
|
char *mount_point = nullptr;
|
||||||
|
|
||||||
mounts = setmntent("/proc/mounts", "r");
|
mounts = setmntent("/proc/mounts", "r");
|
||||||
while (NULL != (ent = getmntent(mounts))) {
|
while (nullptr != (ent = getmntent(mounts))) {
|
||||||
if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
|
if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
|
||||||
{
|
{
|
||||||
mount_point = ent->mnt_dir;
|
mount_point = ent->mnt_dir;
|
||||||
@@ -73,21 +62,12 @@ char *sysfs_read(char *path)
|
|||||||
}
|
}
|
||||||
sysfsFile >> value;
|
sysfsFile >> value;
|
||||||
sysfsFile.close();
|
sysfsFile.close();
|
||||||
|
if (value.empty())
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
char *result = new char[value.length() + 1];
|
char *result = new char[value.length() + 1];
|
||||||
strcpy(result, value.c_str());
|
strcpy(result, value.c_str());
|
||||||
return result;
|
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