disk sector reading in objective c - macos

I am using this method to access drive and reading sector.
int fd = open("/dev/rdisk1s1");
nbytes_got = pread(fd, buf, nbytes, off);
but FD returns -1 means it failed. i need to read disks and their sectors but i am unable to do this in mac OS X. Please help me. Thanks in advance.

If open returns -1, then you need to check errno to see why it failed. The most likely reason is that you are not running your program as root or as a user in group operator.
Also, you are missing the second (oflag) parameter to the open function.
That is pretty basic knowledge. Reading disk sectors directly is generally a pretty advanced topic. You probably shouldn't be doing this at your level of knowledge.

Related

53C94 SCSI IC as target - Cannot handle WRITE(10) command from Initiator

I am building a SD card/SCSI adapter using NCR 53CF94 IC and STM32.
All goes pretty well, even manged to make my device work to some degree, i.e. accepting all basic commands and even booting from it to DOS. BUT i have a problem, when asking initiator (PC) to write something to my device , all goes well i get the block address and the data. Then i write the data to SD card and finally responding with status=0 and message=0 to complete the write command, but the initiaor never increasing the secoor number to continue to write proccess and always tries to write the first one, then after few attempts it gives error on the PC (Error writing to drive...). I can't figure it out why the initiaor is not satisfied with the GOOD status and message. Do i need to send some specific data back to the initiator ? Linke CRC ? Or there is some specific command i need to issue for 53C94 ?
Banging my head for few days now.
Need your assistance please.
Thanks !
Artiom.
I figured it out. I was writing 512 byte blocks to a 256 byte array. I'm not sure how this is related to the issue, but after fixing the size everything started to work.

Generic option bytes on STM32F4

I'm currently tuning some code written for an STM32F070, where we use one byte on user option byte to keep some flags between Resets, using:
FLASH_ProgramOptionByteData(ADRESSE_OPTION_BYTE_DATA0, status_to_store);
Reading carefully the datasheet from our new STM32F446 lets me think that it is no longer possible to use option bytes to store one user byte...
1 - Am I Right with this assertion ? If not, what did I miss ?
2 - Is there some workaround, not involving to rewrite a sector of the flash ?
I am not an expert on stm32, rather still a beginner, but maybe you could have a look on the RTC backup register to hold your data ?

Scratched CD-ROM / Sector by sector reading / SCSI command

I'm trying to recover data from a slightly scratched CD-ROM.
I've tested without success various free or proprietary software (the recover process couldn't even start). I tried to figure out what happened and so far and I've discovered that the size of the CD-ROM was 0. SCSI command READ CD CAPACITY returns 0 Logical Block Addresses / LBA. The CD is not empty, and was not formatted for info.
Consequently all reading access via READ CD SCSI command raise exceptions because asked LBA are out of range.
I also tried SCSI command : READ CD MSF command in order to address directly the physical addresses without considering the logical addresses. Unsuccessfully, it raises the same error out of range error based on LBA ?)
Any idea or help is welcome !
Thanks in advance.

GDB find command error "warning: Unable to access x bytes of target memory at y, halting search"

I'm trying to find current flag count in KMines by using gdb. I know that I should look for memory mappings first to avoid non-existent memory locations. So I ran info proc mappings command to see the memory segments. I picked up a random memory gap (0xd27000-0x168b000) from the result and executed the find command like this: find 0x00d27000, 0x0168b000, 10
But I got the warning: Unable to access 1458 bytes of target memory at 0x168aa4f, halting search. error. Although the address 0x168aa4f is between 0xd27000 and 0x168b000, gdb says that it can't access to it. Why does this happen? What can I do to avoid this situation? Or is there a way to ignore unmapped/unaccessible memory locations?
Edit: I tried to set the value of the address 0x168aa4f to 1 and it works, so gdb can actually access that address but gives error when used with the find command. But why?
I guess I have solved my own problem, I can't believe how simple the solution was. The only thing I did was to decrease the 2nd parameter's value by one. So the code should be find 0x00d27000, 0x0168afff, 10 because linux allocates the memory by using maps in [x,y) format, so if the line in root/proc/pid/maps says something like this;
01a03000-0222a000 rw-p
The memory allocated includes 0x01a03000 but not 0x0222a000. Hope this silly mistake of mine helps someone :D
Edit: The root of the problem is the algorithm implemented in target.c(gdb's source code I mean) the algorithm reads and searches the memory as chunks at the size of 16000 bytes. So even if the last byte of the chunk is invalid, gdb will throw the entire chunk into the trash and won't even give any proper information about the invalid byte, it only reports the beginning of the current chunk.

Windows device driver: FSCTL_LOCK_VOLUME on raw partition - Access is Denied

I am currently writing a Windows utility that does a similar job as Linux dd. But this utility only copy USED block instead of cloning the whole disk block by block.
The way I write it is to
*
(1) Copy MBR from disk A which contains three partitions to disk B.
(2) after step A. I am able see three raw partitions from disk management GUI. (no drive letter, no file system).
(3) Copy each partitions from A to B in a loop.*
The problem I have is:
in step (3) after I get the file handle from OpenDevice and I try to use that handle to lock a raw partition, I get Access Denied (error 5) and I am 100% sure that my program did not accidentally hold the lock somewhere else. My dev and testing environment is Windows 2003 server and my utility is executed as Admin privilege.
Here is the code snippet:
...
shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
fHandle= OpenDevice(shareMode);
res = DeviceIoControl(fHandle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dummy, NULL);
if (res == 0)
{
DismountAndLockVolume();
}
...
in my OpenDevice function in use:
DWORD access = (fOpenMode==forWriting) ? (GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE) : GENERIC_READ| SYNCHRONIZE;
NTOpen(&fHandle, fName.c_str(), access, FILE_ATTRIBUTE_NORMAL, shareMode, FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT|FILE_RANDOM_ACCESS|FILE_NON_DIRECTORY_FILE);
to get the file handle(fHandle).
In DismountAndLockVolume function, I try to dismount this volume and grab the lock on it again.
DeviceIoControl(fHandle, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dummy, NULL);
DWORD shareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ;
ntStatus = OpenDevice(shareMode);
res = DeviceIoControl(fHandle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dummy, NULL);
The problem is, when I my code try to dismount this volume, an exception is thrown due to access denied.
Then I browsed the Internet and get the following discussion thread: QUOTE
A write on a volume handle will succeed if the volume is not mounted by a
file system, or if one of the following conditions is true:
1. The sectors to be written to are boot sectors.
2. The sectors to be written to reside outside of file system space.
3. You have explicitly locked or dismounted the volume by using
FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
4 . The volume has no file system. (In other words, it has been mounted
as a RAW volume.)
A write on a disk handle will succeed if one of the following conditions
is true:
1. The sectors to be written to do not fall within a volume's extents.
2. The sectors to be written to fall within a mounted volume, but you
have explicitly locked or dismounted the volume by using FSCTL_LOCK_VOLUME
or FSCTL_DISMOUNT_VOLUME.
3. The sectors to be written to fall within a volume that is not mounted
or has no file system.
The modification of some disk parts, like the boot sector ( upto 16 ), is
still allowed . But my utility relies on raw write access to the disk. I am
not able to lock volume by FSCTL_LOCK_VOLUME. Result is ACCESS DENIED. While
searching through net i come to know that kernel mode driver is only
solution. But in this group i come to know driver is not require. I work on
services and pass through SCSI, but i am not able to find the solution. If
kernel mode driver is require then what kind of driver it should be ?
Is anybody know the solution of the problem then please help me out
This thread terminates with no conclusion. Can somebody give me a hand on what was going on? Any hint for possible solution?
Millions of Thanks. I have been scratching my hair for days :-(((.

Resources