Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lab8/archive/app4/app4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "inc/syscall.h"

int main(int argc,char** argv){
int fd=open(argv[1],0);
char name[100];
int size;
while(1){
size=read(fd,name,100);
name[size]=0;
if(size==0)break;
uart_printf("Name: %s, Size: %d\n",name,size);
}

exit();
}
150 changes: 150 additions & 0 deletions Lab8/archive/app4/inc/printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/**
* minimal sprintf implementation
*/
unsigned int vsprintf(char *dst, char* fmt, __builtin_va_list args)
{
long int arg;
int len, sign, i;
char *p, *orig=dst, tmpstr[19];

// failsafes
if(dst==(void*)0 || fmt==(void*)0) {
return 0;
}

// main loop
arg = 0;
while(*fmt) {
// argument access
if(*fmt=='%') {
fmt++;
// literal %
if(*fmt=='%') {
goto put;
}
len=0;
// size modifier
while(*fmt>='0' && *fmt<='9') {
len *= 10;
len += *fmt-'0';
fmt++;
}
// skip long modifier
if(*fmt=='l') {
fmt++;
}
// character
if(*fmt=='c') {
arg = __builtin_va_arg(args, int);
*dst++ = (char)arg;
fmt++;
continue;
} else
// decimal number
if(*fmt=='d') {
arg = __builtin_va_arg(args, int);
// check input
sign=0;
if((int)arg<0) {
arg*=-1;
sign++;
}
if(arg>99999999999999999L) {
arg=99999999999999999L;
}
// convert to string
i=18;
tmpstr[i]=0;
do {
tmpstr[--i]='0'+(arg%10);
arg/=10;
} while(arg!=0 && i>0);
if(sign) {
tmpstr[--i]='-';
}
// padding, only space
if(len>0 && len<18) {
while(i>18-len) {
tmpstr[--i]=' ';
}
}
p=&tmpstr[i];
goto copystring;
} else
// hex number
if(*fmt=='x') {
arg = __builtin_va_arg(args, long int);
// convert to string
i=16;
tmpstr[i]=0;
do {
char n=arg & 0xf;
// 0-9 => '0'-'9', 10-15 => 'A'-'F'
tmpstr[--i]=n+(n>9?0x37:0x30);
arg>>=4;
} while(arg!=0 && i>0);
// padding, only leading zeros
if(len>0 && len<=16) {
while(i>16-len) {
tmpstr[--i]='0';
}
}
p=&tmpstr[i];
goto copystring;
} else
// string
if(*fmt=='s') {
p = __builtin_va_arg(args, char*);
copystring: if(p==(void*)0) {
p="(null)";
}
while(*p) {
*dst++ = *p++;
}
}
} else {
put: *dst++ = *fmt;
}
fmt++;
}
*dst=0;
// number of bytes written
return dst-orig;
}

/**
* Variable length arguments
*/
unsigned int sprintf(char *dst, char* fmt, ...)
{
//__builtin_va_start(args, fmt): "..." is pointed by args
//__builtin_va_arg(args,int): ret=(int)*args;args++;return ret;
__builtin_va_list args;
__builtin_va_start(args, fmt);
return vsprintf(dst,fmt,args);
}
27 changes: 27 additions & 0 deletions Lab8/archive/app4/inc/printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

unsigned int sprintf(char *dst, char* fmt, ...);
unsigned int vsprintf(char *dst,char* fmt, __builtin_va_list args);
103 changes: 103 additions & 0 deletions Lab8/archive/app4/inc/syscall.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "syscall.h"
#include "printf.h"

void delay(int cnt){
while(cnt>0)cnt--;
}

int getpid(){
long ret;
asm volatile("\
svc 1\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int uart_read(char* buf,int size){
long ret;
asm volatile("\
svc 2\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int uart_write(char* buf,int size){
long ret;
asm volatile("\
svc 3\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

unsigned int uart_printf(char* fmt,...){
char dst[100];
//__builtin_va_start(args, fmt): "..." is pointed by args
//__builtin_va_arg(args,int): ret=(int)*args;args++;return ret;
__builtin_va_list args;
__builtin_va_start(args,fmt);
unsigned int ret=vsprintf(dst,fmt,args);
uart_write(dst,ret);
return ret;
}

int exec(char* name,char** argv){
long ret;
asm volatile("\
svc 4\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

void exit(){
asm volatile("svc 5\n"::);
while(1){}
}

int fork(){
long ret;
asm volatile("\
svc 6\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int open(const char *pathname,int flags){
long ret;
asm volatile("\
svc 7\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int close(int fd){
long ret;
asm volatile("\
svc 8\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int write(int fd,const void *buf,int count){
long ret;
asm volatile("\
svc 9\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}

int read(int fd,void *buf,int count){
long ret;
asm volatile("\
svc 10\n\
mov %0, x0\n\
":"=r"(ret):);
return ret;
}
14 changes: 14 additions & 0 deletions Lab8/archive/app4/inc/syscall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define O_CREAT 2

void delay(int cnt);
int getpid();
int uart_read(char* buf,int size);
int uart_write(char* buf,int size);
unsigned int uart_printf(char* fmt,...);
int exec(char* name,char** argv);
void exit();
int fork();
int open(const char *pathname,int flags);
int close(int fd);
int write(int fd,const void *buf,int count);
int read(int fd,void *buf,int count);
16 changes: 16 additions & 0 deletions Lab8/archive/app4/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SECTIONS
{
. = 0x70000;
__prog_start = .;
.text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) }
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) }
.data : { *(.data .data.* .gnu.linkonce.d*) }
.bss (NOLOAD) : {
. = ALIGN(16);
__bss_start = .;
*(.bss .bss.*)
*(COMMON)
__bss_end = .;
}
}
__bss_size = (__bss_end - __bss_start)>>3;
25 changes: 25 additions & 0 deletions Lab8/archive/app4/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY:all clean
APP:=app4
LINKER:=linker.ld
CFLAGS:=-nostdinc -nostdlib -nostartfiles
LIBS:=$(wildcard inc/*.c)
OBJS:=$(APP).o $(LIBS:.c=.o)

all:
make $(APP)
cp $(APP) ../rootfs/$(APP)
cd ..&&make

$(APP):$(OBJS)
aarch64-linux-gnu-ld -T $(LINKER) -o tmp.elf $(OBJS)
aarch64-linux-gnu-objcopy -O binary tmp.elf $@
rm tmp.elf

%.o:%.c
aarch64-linux-gnu-gcc $(CFLAGS) -c $< -o $@
aarch64-linux-gnu-gcc $(CFLAGS) -S $<

clean:
-rm $(APP)
-rm $(OBJS)
-rm *.s
22 changes: 22 additions & 0 deletions Lab8/archive/app5/app5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "inc/syscall.h"

int main(int argc,char** argv){
char buffer[101];

int fd=open(argv[1],0);
uart_printf("read somthing from %s(fd %d):\n",argv[1],fd);
while(1){
int ret=read(fd,buffer,100);
if(ret==0)break;
buffer[ret]=0;
uart_printf("%s",buffer);
}
close(fd);

fd=open(argv[1],0);
uart_printf("\nwrite somthing to %s(fd %d):\n",argv[1],fd);
int len=uart_read(buffer,100);
write(fd,buffer,len);

exit();
}
Loading