Using peek.c as a starting point create a new C application to perform the reading of a string from the PL address space.
steve@Desktop:~/projects/zedboard_leds_switches$ cd ../zedboard_linux
Something similar to the following should do the trick.
steve@Desktop:~/projects/zedboard_linux$ subl os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/peekstring.c
//
// File .......... peekstring.c
// Author ........ Steve Haywood
// Version ....... 1.0
// Date .......... 29 October 2021
// Description ...
// CGI type module to read a string from memory and return it.
// Usage ...
// http://<web address>/cgi-bin/peekstring?base&size&offset&maxchars
// base = Base address of memory region to be used
// size = Size of memory region to be used
// offset = Offset address of string within memory region
// maxchars = Maximum length of return string
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdint.h>
#include <unistd.h>
int main()
{
int fd; // File descriptor for mapped memory
char *map; // Pointer to mapped memory
uint32_t base; // Start address of area to map
uint32_t size; // Size of area to map
uint32_t addr; // Address within area to access
uint32_t length; // Maximum length of string to return
char *ptr; // Pointer to character of string
char *querystring; // Pointer to QUERY_STRING
char *rest; // Pointer to next token within QUERY_STRING
char *token; // Pointer to extracted token
char *endptr = NULL; // Pointer to end of string in str2int conversion
uint32_t count = 0; // Number of character output from string
printf("Content-Type: text/plain;charset=us-ascii\n\n");
querystring = getenv("QUERY_STRING");
if (querystring)
{
rest = querystring;
token = strtok_r(rest, "&", &rest);
if (token != NULL) {
base = strtoul(token, &endptr, 0);
if (token != endptr) {
token = strtok_r(rest, "&", &rest);
if (token != NULL) {
size = strtoul(token, &endptr, 0);
if (token != endptr) {
token = strtok_r(rest, "&", &rest);
if (token != NULL) {
addr = strtoul(token, &endptr, 0);
if (token != endptr) {
token = strtok_r(rest, "&", &rest);
if (token != NULL) {
length = strtoul(token, &endptr, 0);
if (token != endptr) {
fd = open( "/dev/mem", O_RDWR);
if (fd > 0) {
map = mmap(NULL, size, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, base);
if (map != MAP_FAILED) {
ptr = (char *)(map + addr);
while (*ptr != '\0' && count < length) {
printf("%c", *ptr);
ptr++;
count++;
}
munmap(map, size);
} else printf("Error: Memory to mmap");
close(fd);
} else printf("Error: Failed to open /dev/mem");
} else printf("Error: Invalid string length");
} else printf("Error: Missing string length");
} else printf("Error: Invalid string offset address");
} else printf("Error: Missing string offset address");
} else printf("Error: Invalid memory map size");
} else printf("Error: Missing memory map size");
} else printf("Error: Invalid memory map base address");
} else printf("Error: Missing memory map base address");
} else printf("Error: No QUERY_STRING");
}
Direct download available here :-
steve@Desktop:~/projects/zedboard_linux$ wget https://spacewire.co.uk/tutorial/shared/repos/0017/zedboard_linux/os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/peekstring.c -O os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/peekstring.c
Modify the Makefile to include the peekstring application.
steve@Desktop:~/projects/zedboard_linux$ subl os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/Makefile
PEEK = peek
POKE = poke
PEEKSTRING = peekstring
# Add any other object files to this list below
PEEK_OBJS = peek.o
POKE_OBJS = poke.o
PEEKSTRING_OBJS = peekstring.o
all: $(PEEK) $(POKE) $(PEEKSTRING)
$(PEEKSTRING): $(PEEKSTRING_OBJS)
$(CC) $(LDFLAGS) -o $@ $(PEEKSTRING_OBJS) $(LDLIBS)
$(POKE): $(POKE_OBJS)
$(CC) $(LDFLAGS) -o $@ $(POKE_OBJS) $(LDLIBS)
$(PEEK): $(PEEK_OBJS)
$(CC) $(LDFLAGS) -o $@ $(PEEK_OBJS) $(LDLIBS)
clean:
-rm -f $(PEEKSTRING) $(POKE) $(PEEK) *.elf *.gdb *.o
Direct download available here :-
steve@Desktop:~/projects/zedboard_linux$ wget https://spacewire.co.uk/tutorial/shared/repos/0017/zedboard_linux/os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/Makefile -O os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/Makefile
Check out the changes.
steve@Desktop:~/projects/zedboard_linux$ git difftool os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/files/Makefile
Modify the BitBake recipe to include the peekstring application.
steve@Desktop:~/projects/zedboard_linux$ subl os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/peekpokecgi.bb
#
# This is the peekpokecgi aplication recipe
#
#
SUMMARY = "peekpokecgi application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://peek.c \
file://poke.c \
file://peekstring.c \
file://Makefile \
"
FILES_${PN} += "/srv/www/cgi-bin"
S = "${WORKDIR}"
CFLAGS_prepend = "-I ${S}/include"
do_compile() {
oe_runmake
}
do_install() {
install -d ${D}/srv/www/cgi-bin
install -m 0755 ${S}/peek ${D}/srv/www/cgi-bin
install -m 0755 ${S}/poke ${D}/srv/www/cgi-bin
install -m 0755 ${S}/peekstring ${D}/srv/www/cgi-bin
}
Direct download available here :-
steve@Desktop:~/projects/zedboard_linux$ wget https://spacewire.co.uk/tutorial/shared/repos/0017/zedboard_linux/os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/peekpokecgi.bb -O os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/peekpokecgi.bb
Check out the changes.
steve@Desktop:~/projects/zedboard_linux$ git difftool os/petalinux/project-spec/meta-user/recipes-apps/peekpokecgi/peekpokecgi.bb