Mounting a read only drive as write - linux

I have a special bespoke device with a USB interface. When plugging in the device to my laptop - Ubuntu 12.04 it mounts as a read only USB drive - with a file on it. This file is created by the device and writes to the file when the device scans stuff.
I however, want to be able to write to the drive so the device 'thinks' it has already scanned x amount of entries.
Basically I want to replace the file 'File1.txt' my version of 'File1.txt' however I cannot because the drive is mounting as Read only.
I have tried the following commands:
andy#andy-ThinkPad-W530:/media/iRead$ touch giveme.txt
touch: cannot touch `giveme.txt': Read-only file system
andy#andy-ThinkPad-W530:/media/iRead$ sudo mount -o remount,rw '/media/iRead'
[sudo] password for andy:
mount: cannot remount block device /dev/sdb read-write, is write-protected
andy#andy-ThinkPad-W530:/media/iRead$
Can anybody suggest anything I can try to mount this as writable drive?
I have a very strong feeling that the chip which is storing this data is the following:
ARM STM32F103 RBT6 22oUP _ 93 MLT22950
Hope this helps somebody to help me!

If processing write commands coming from USB wasn't part of the requirements, it's very unlikely that the device processes write commands.
A read-only USB mass storage device is not a read-write mass storage device with write-protection slapped on top. It's a USB device that doesn't have logic for understanding write requests at all.
After the device is finished and delivered is a little late for deciding
I want to be able to write to the drive so the device 'thinks' it has already scanned x amount of entries.
Of course the flash memory used inside the device is written during its operation. But the way data is stored inside might not look anything like its USB presentation, and the conversion is most likely one-way only.
Since the developer probably did not implement mass storage support from scratch, and the library they used probably has write support, they may be able to easily supply you with firmware modified to be writable and do something with the written data. But without changing the firmware, you get nowhere.

Related

USB drive file access history

I recently forgot my USB drive at work and a collegue found it. I got the drive after 4 days. I have a feeling my colleague accesed or copied my personal files.
How can I find out?
You have no means to know whether he copied the files or not,as such auditing can be enforced only with particular domain policies on machines where users have no privileges.
For example, you coworker could have unplugged the USB stick, and plug it on his personal computer. It worse, if he had Linux, just mounted the USB stick in read only to prevent timestamp changes.
Note that on windows you can do the same. To prevent thefts, you should encrypt the USB stick.

opendir for usb device /dev/sdb

I am trying to implement in c program a way to detect if usb is connected or not.
I noticed that when the usb is connected, then the following command from shell will result as following:
root:~# ls /dev/sdb
/dev/sdb
If usb is disconnected then I get
root:~# ls /dev/sdb
ls: /dev/sdb: No such file or directory
I therefore assumed that best way to detect usb connected from c program shall be by doing opendir("/dev/sdb"), but the open call is always failed.
Do you have any idea what's best methd to do this detection ?
The main goal, is knowing in run-time program where the udev mounted the harddisk, and where the usb flash drive (if plugged in).
Thanks,
Ran
A) Why your opendir call failed? It's easy to understand. Because /dev/sdb is not a directory. For me it's hard to understand what you expect, when you try to open device as a directory. If you want to get file list from you usb drive, you need to mount the partition (/dev/sdb1, /dev/sdb2, etc. not the /dev/sdb) to some mount point (directory). And if you mount it successfully, then you can open the directory with your call. Many linux distributions mount usb drive automatically. You can look to your distribution documentation to get the information about automatical mounting of the usb drives.
B) I think it's easy to understand why detecting the usb device this way is bad idea.
=> Different computers have different amount of drives. So on many computers
/dev/sdb - is a hard drive.
=> Asuming you know that there is only one hard drive, the disk naming still can change after computer reboot, so it's possible that after reboot the usb drive will become sda and hard drive will become sdb (but usually this doesn't happen).
=> Generally, it's not possible to predict a new letter (sdb or sdc or sde).
=> You can't access to the usb devices, that are not usb drives.
=> There are tonns of other problems with such solution.
C) I didn't understand your goal completely, but if you want your program to be make some action if the usb device is plugged in, that the best solution you can do is read about udev and the event system of your distribution. If you want to make with your usb device some low level operations, you can read about libusb. You can get the general information about usb devices with lsusb command which is usually a part of a distributions. You can google for some other infermer commands.
The device /dev/sdb is not a directory, it's just a device, so you should use a system call like stat that will tell you whether a file exists instead of trying to calling opendir on it.
Please note that /dev/sdb could be any kind of hard drive depending on the setup of your specific system; it is not necessarily a USB drive.
Alternatively, you could execute the lsusb utility and parse its output.

How to intercept data sent to a USB flash drive in the Linux kernel?

Where in the Linux kernel source code can I find the function(s) that deal with sending data to a USB flash drive? By "data", I mean the actual file contents. For example, when I drag and drop "Report.docx" to the USB thumb drive, I need to be able to intercept this data to the point where I could modify the contents of the file before it is written to the USB thumb drive.
I understand that the USB storage module (drivers/usb/storage) is where USB mass storage devices like thumb drives are handled, but I failed to find the right place to look. I looked around transfer.c but my debugging attempts just show the functions get called whether or not I'm transferring any files. Also, from my understanding, everything is done with URBs and I'm not clear on how to deal with those. Am I looking in the right place?
For the purposes of my project, I need to modify the driver (not using any third-party libraries or user-space code). I am working with Linux 3.12.0.
Not an expert in usb subsystem, but I would start by looking at
drivers/usb/storage/transport.c::usb_stor_bulk_transfer_buf()

Detect a cryptofs device or partition

I'm trying to develop an automount for cryptofs encrypted devices/partitions. The thing is that I don't have experience in the low level layer of Linux.
Is there any way I can detect when a cryptofs device or partition has been inserted in the system? (p.e. when you insert a dongle with a regular partition and an encrypted one)
Never tried but I would follow this approach:
In Linux plug and play is handled by hal and/or udev. hal is bit older and most of the recent distributions uses udev.
You can start looking into "libudev". Using libudev api's you will be able to get the information about connected devices.
This should help: http://www.signal11.us/oss/udev/
After that, open the device and start reading the filesystem information and figure out if it is cryptofs
See, if this answer helps: How to programmatically discover the filesystem without mounting the device (like "fdisk -l")

How can I list the contents of an SD card connected to Linux via a phone in USB Mass Storage Mode?

Is it possible to get a list of files from an SD card when it is connected as a USB Mass Storage device?
UMS provides the contents of the SD card as a raw storage device, much the same as a hard drive or CD driver. As such, it is up to the operating system's VFS to make sense of the partitioning, formatting, and directory structure contained on it.

Resources