access file in mips using mars tool - mips

I'm trying to do some access on file using mips instruction.
I want to read file line by line not all of the file at a time so this code(1) does not work.
Also I want to write to the file and not overwrite!
Can any one help me?
Code:
Open a file for writing
li $v0, 13 # system call for open file
la $a0, file # board file name
li $a1, 0 # Open for reading
li $a2, 0
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
Read from file
li $v0, 14 # system call for read from file
move $a0, $s6 # file descriptor
la $a1, buffer # address of buffer to which to read
li $a2, 40 # hardcoded buffer length
syscall # read from file
Close the file
li $v0, 16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file

I want to read file line by line not all of the file at a time so this code(1) does not work.
Read chunks of data into a buffer (e.g. a few kilobytes). Then process that buffer line-by-line (by looking for linefeed characters), and read more data from the file when you've reached the end of the buffer.
Also I want to write to the file and not overwrite!
Set the flags ($a1) to 9 when opening the file (syscall 13). This corresponds to "write-only with create and append" (see this MARS syscall reference).

it's work thx alot :)
.data
fin: .asciiz "file.txt" # filename for input
buffer: .space 128
buffer1: .asciiz "\n"
val : .space 128
.text
################################################ fileRead:
# Open file for reading
li $v0, 13 # system call for open file
la $a0, fin # input file name
li $a1, 0 # flag for reading
li $a2, 0 # mode is ignored
syscall # open a file
move $s0, $v0 # save the file descriptor
# reading from file just opened
li $v0, 14 # system call for reading from file
move $a0, $s0 # file descriptor
la $a1, buffer # address of buffer from which to read
li $a2, 6 # hardcoded buffer length
syscall # read from file
li $v0, 4 #
la $a0, buffer # buffer contains the values
syscall # print int
lb $t1 , buffer
# reading from file just opened
li $v0, 14 # system call for reading from file
move $a0, $s0 # file descriptor
la $a1, buffer # address of buffer from which to read
li $a2, 6 # hardcoded buffer length
syscall # read from file
li $v0, 4 #
la $a0, buffer # buffer contains the values
syscall # print int
## sh $t5 , val($0) #sw $t5, theArray($t0)
# Close the file
li $v0, 16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file

Related

qemu aarch64 gicv3 memory-mapped registers can't be modified

According to the info in generated dtb, the memory-mapped registers of gicv3-distributor have a base addr, which is 0x0800_0000.
And I have checked the validity by reading gicd_typer, which means the base addr is right.
But when I want to configure the gicv3-distributor (like changing GICD_ICACTIVER<n>), the value is not changed, keeping the default value. The same thing happens on any register of GICD in my machine.
I change them with assembly code as soon as the machine starts, not enable MMU.
ldr x8, =0x8000380
ldr x9, =0xffffffff
str x9, [x8]
dsb sy
ldr x0, [x8]
(gdb) i r x0
x0 0x0 0
Even I write to this memory by gdb set command, the value is also unchangeable.
The addr of GICD_ICACTIVER<n> should be 0x0800_0380(offset=0x380), which should be readable and writable, isn't it?
The UART(pl011)-related address(0x0900_0000) can be written normally.
Host machine: Ubuntu 18.04
qemu version: 7.1.50 (v7.1.0-256-g79dfa177)
Target OS: bare-metal
qemu command:
./qemu-system-aarch64 -machine virt,gic-version=3,secure=on,virtualization=on -cpu cortex-a76 -m 1G
The GICD_ICACTIVER register bits are write-1-to-clear, so if you write all-1s to the register then you should expect it to read back as all-0s.
More generally, not all hardware device registers will read back the same value that you write to them -- it depends entirely on the device.

MIPS won't read my .txt file to the buffer

I've been trying to get this code working for quite some time now. Is there anyone that can explain to me why the buffer stays empty after the syscall. The .txt file, the .asm file and the mars.jar are all in the same directory. I've tried specifying a full path to the file but that didn't work either.
.data
fin: .asciiz "input.txt"
.align 2
buffer: .space 2048
.text
# Open file
li $v0, 13 # System call for opening files
la $a0, fin # load file name adress in $a0
li $a1, 0 # Open for writing
li $a2, 0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s3, $v0 # save file descriptor to $s3
# Read from file to buffer
li $v0, 14 # system call for read from file
move $a0, $s3 # file descriptor
la $a1, buffer # address of buffer to which to load the contents
li $a2, 2048 # hardcoded max number of characters
syscall # read file
# Close file
li $v0, 16 # system call for close file
move $a0, $s3 # file descriptor to close
syscall # close file
I've found the solution. You have to specify the full path from root to file to get it to work. Hope this helps someone else in the future.

can't run '/etc/init.d/rcS': No such file or directory

I am trying to emulate a firmware image using qemu. During booting, I get the following error
can't run '/etc/init.d/rcS': No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
.
.
.
This is the content of the inittab file
# Startup the system
null::sysinit:/etc/init.d/rc.sysinit
# now run any rc scripts
::sysinit:/etc/init.d/rcS
# Put a getty on the serial port
ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100
# Stuff to do before rebooting
null::shutdown:/bin/umount -a -r
It is able to run the rc.sysinit, but not the rcS.
I have checked permissions of the rcS. Also, the filesystem is mounted as read-only cramfs. Could this be causing an issue?
This is the command I am running:
QEMU_AUDIO_DRV=none \qemu-system-arm -m 256M -M versatilepb
-kernel ~/linux-2.6.23/arch/arm/boot/zImage
-append "console=ttyAMA0,115200 root=/dev/ram rdinit=/sbin/init"
-initrd ~/tmpcramfs2
-nographic
These are the boot messages obtained on running the command:
Linux version 2.6.23 (hsailer#SvanteArrhenius) (gcc version 4.0.2) #1 Thu May 27 09:31:10 EDT 2021
CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=00093177
Machine: ARM-Versatile PB
Memory policy: ECC disabled, Data cache writeback
CPU0: D VIVT write-through cache
CPU0: I cache: 4096 bytes, associativity 4, 32 byte lines, 32 sets
CPU0: D cache: 65536 bytes, associativity 4, 32 byte lines, 512 sets
Built 1 zonelists in Zone order. Total pages: 65024
Kernel command line: console=ttyAMA0,115200 root=/dev/ram rdinit=/sbin/init
PID hash table entries: 1024 (order: 10, 4096 bytes)
Console: colour dummy device 80x30
Dentry cache hash table entries: 32768 (order: 5, 131072 bytes)
Inode-cache hash table entries: 16384 (order: 4, 65536 bytes)
Memory: 256MB = 256MB total
Memory: 249600KB available (2508K code, 227K data, 100K init)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
NET: Registered protocol family 16
NET: Registered protocol family 2
Time: timer3 clocksource has been installed.
IP route cache hash table entries: 2048 (order: 1, 8192 bytes)
TCP established hash table entries: 8192 (order: 4, 65536 bytes)
TCP bind hash table entries: 8192 (order: 3, 32768 bytes)
TCP: Hash tables configured (established 8192 bind 8192)
TCP reno registered
checking if image is initramfs...it isn't (bad gzip magic numbers); looks like an initrd
Freeing initrd memory: 7184K
NetWinder Floating Point Emulator V0.97 (double precision)
Installing knfsd (copyright (C) 1996 okir#monad.swb.de).
JFFS2 version 2.2. (NAND) © 2001-2006 Red Hat, Inc.
JFS: nTxBlock = 2007, nTxLock = 16063
io scheduler noop registered
io scheduler anticipatory registered (default)
io scheduler deadline registered
io scheduler cfq registered
CLCD: Versatile hardware, VGA display
Clock CLCDCLK: setting VCO reg params: S=1 R=99 V=98
Console: switching to colour frame buffer device 80x60
Serial: AMBA PL011 UART driver
dev:f1: ttyAMA0 at MMIO 0x101f1000 (irq = 12) is a AMBA/PL011
console [ttyAMA0] enabled
dev:f2: ttyAMA1 at MMIO 0x101f2000 (irq = 13) is a AMBA/PL011
dev:f3: ttyAMA2 at MMIO 0x101f3000 (irq = 14) is a AMBA/PL011
fpga:09: ttyAMA3 at MMIO 0x10009000 (irq = 38) is a AMBA/PL011
RAMDISK driver initialized: 16 RAM disks of 8192K size 1024 blocksize
smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico#cam.org>
eth0: SMC91C11xFD (rev 1) at d098e000 IRQ 25 [nowait]
eth0: Ethernet addr: 52:54:00:12:34:56
armflash.0: Found 1 x32 devices at 0x0 in 32-bit bank
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
RedBoot partition parsing not available
afs partition parsing not available
armflash: probe of armflash.0 failed with error -22
mice: PS/2 mouse device common for all mice
input: AT Raw Set 2 keyboard as /class/input/input0
TCP cubic registered
NET: Registered protocol family 1
NET: Registered protocol family 17
VFP support v0.3: implementor 41 architecture 1 part 10 variant 9 rev 0
input: ImExPS/2 Generic Explorer Mouse as /class/input/input1
RAMDISK: cramfs filesystem found at block 0
RAMDISK: Loading 7184KiB [1 disk] into ram disk... done.
VFS: Mounted root (cramfs filesystem) readonly.
Freeing init memory: 100K
can't run '/etc/init.d/rcS': No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
.
.
.
The errors about /dev/ttyS0 are because your inittab is specifying the wrong device name for the serial port for the (emulated) hardware you're running on. Your QEMU command specifies the 'versatilepb' board, whose serial devices are PL011s, which appear in /dev/ as /dev/ttyAMA0, /dev/ttyAMA1, etc. (/dev/ttyS0 is what the serial ports on an x86 PC appear as.) You need to fix that line of the inittab to refer to ttyAMA0 instead.
For the rcS error, I would suggest you start by double-checking all the things listed in all the responses to this older question.

caffe - How can I reduce size of input images for googlenet training

I am using googlenet model and I want to use a different image size for training (a smaller version of the image) , the image_mean and lmdb files have the reduced size as well . When I modified the size in the googlenet.prototxt and ran the train.sh file the program is being aborted with the following error.
Check failure stack trace: ***
# 0x7fdbb9499daa (unknown)
# 0x7fdbb9499ce4 (unknown)
# 0x7fdbb94996e6 (unknown)
# 0x7fdbb949c687 (unknown)
# 0x7fdbb99543ee caffe::Blob<>::Reshape()
# 0x7fdbb9954619 caffe::Blob<>::Reshape()
# 0x7fdbb98dcc08 caffe::PoolingLayer<>::Reshape()
# 0x7fdbb99968ac caffe::Net<>::Init()
# 0x7fdbb9997725 caffe::Net<>::Net()
# 0x7fdbb985174a caffe::Solver<>::InitTrainNet()
# 0x7fdbb985284c caffe::Solver<>::Init()
# 0x7fdbb9852b7a caffe::Solver<>::Solver()
# 0x7fdbb984a263 caffe::Creator_SGDSolver<>()
# 0x40e67e caffe::SolverRegistry<>::CreateSolver()
# 0x40794b train()
# 0x40590c main
# 0x7fdbb87a7f45 (unknown)
# 0x406041 (unknown)
# (nil) (unknown) Aborted (core dumped)
I have found out that the size for googlenet model is fixed and unique , so modifying it will result in an error. So its mandatory to maintain the same size as required by the model to train.

Caffe: can't open imagenet_mean_test.binaryproto

Upon running ./train_caffenet.sh; creating the layer data, setting up the data, loading the training mean.binaryproto file and opening the training lmdb file all works.
However when it comes to loading the test mean.binaryproto file for the test data I get the following error:
Loading mean file from: /home/pwhc/caffe/Learn/imagenet_mean_test.binaryproto
F0716 13:12:13.917732 3385 db.hpp:109] Check failed: mdb_status == 0 (2 vs. 0) No such file or directory
*** Check failure stack trace: ***
# 0x7f8337946daa (unknown)
# 0x7f8337946ce4 (unknown)
# 0x7f83379466e6 (unknown)
# 0x7f8337949687 (unknown)
# 0x7f8337cbf5be caffe::db::LMDB::Open()
# 0x7f8337d16b82 caffe::DataLayer<>::DataLayerSetUp()
# 0x7f8337d806f9 caffe::BasePrefetchingDataLayer<>::LayerSetUp()
# 0x7f8337ca3db3 caffe::Net<>::Init()
# 0x7f8337ca5b22 caffe::Net<>::Net()
# 0x7f8337cb0a24 caffe::Solver<>::InitTestNets()
# 0x7f8337cb111b caffe::Solver<>::Init()
# 0x7f8337cb12e6 caffe::Solver<>::Solver()
# 0x40c4c0 caffe::GetSolver<>()
# 0x406503 train()
# 0x404ab1 main
# 0x7f8336e58ec5 (unknown)
# 0x40505d (unknown)
# (nil) (unknown)
Aborted (core dumped)
I modified the train_val.prototxt to point the to appropriate mean.binaryproto files (using absolute paths) and have checked and double checked to make sure everything matches.
Any thoughts would be greatly appreciated.
See my answer here:
https://github.com/BVLC/caffe/issues/2780#issuecomment-123385714
Can you post your data layers? It seems like you've switched up data_param.source and transform_param.mean_file.
When you creating new LMDB database from image net, please delete the previous LMDB. This error will come to happen when writing the new image net data for existing LMDB database.