The only way to open a file for reading is using the read-write mode and then seeking back to position 0 in the file.
Not working code using O_RDONLY
.data
filename: .string "/tmp/test.txt"
.text
la a0 filename # a0: Pointer to filename
li a1 0 # Readonly flag
li a7 1024 # "Open" ecall
ecall # Returns: File descriptor
li a1 0x2000 # a1: buffer
li a2 100 # a2: max. buffer size
li a7 63 # "Read" ecall
ecall
li a0 0x2000
li a7 4 # "Print String" ecall
ecall
Workaround using O_RDWR and seek
.data
filename: .string "/tmp/test.txt"
.text
la a0 filename # a0: Pointer to filename
li a1 2 # Read-Write flag
li a7 1024 # "Open" ecall
ecall # Returns: File descriptor
mv s0 a0 # Save fd, since it will be overwritten by return values
nop # file descriptor stays in a0
li a1 0 # Offset 0
li a2 0 # From the beginning
li a7 62 # "Seek" ecall
ecall
mv a0 s0 # Get back file descriptor
li a1 0x2000 # a1: buffer
li a2 100 # a2: max. buffer size
li a7 63 # "Read" ecall
ecall
li a0 0x2000
li a7 4 # "Print String" ecall
ecall
It is not possible to open a file in read only mode by passing
0
asflags
parameter. The file will have the stateQIODevice::NotOpen
instead.This seems to be related to a bug in the implementation of the ecall. Parsing the flags does not work for a
0
flag, asflags & O_RDONLY
always evaluates to false sinceO_RDONLY
equals0
:https://github.com/mortbopet/Ripes/blob/27fcc58c02b26e84156ef588d42fb36d8ada4543/src/syscall/systemio.h#L149-L154
The only way to open a file for reading is using the read-write mode and then seeking back to position 0 in the file.
Not working code using
O_RDONLY
Workaround using
O_RDWR
and seek