Zynq design from scratch. Part 35.
Using CGI scriptsThe Common Gateway Interface (CGI) is a standard method for web servers software to delegate the generation of web pages to executable files. Such files are known as CGI scripts; they are programs, often stand-alone applications, usually written in a scripting language like Perl. In this example we will write the CGI script using the C language.
Create a CGI script
Create a user application by running petalinux-create -t apps from inside a PetaLinux project on our workstation.
->cd Projects/PetaLinux/Avnet-Digilent-ZedBoard-2013.3
->petalinux-create -t apps --name HelloWorld
HelloWorld C program
As usual when starting work with some new programming technology, we should probably first make a trivial program work. This avoids fighting with many potential problems at a time and concentrating first on the issues specific to the environment, here CGI.
We will use the following program that just prints Hello world but preceded by HTTP headers as required by the CGI interface. Here the header specifies that the data is plain ASCII text.
#include <stdio.h>
int main(void) {
printf("Content-Type: text/plain;charset=us-ascii ");
printf("Hello world ");
return 0;
}
HelloWorld makefile
The CGI script will be installed in directory: /home/httpd/cgi-bin
APP = HelloWorld
# Add any other object files to this list below
APP_OBJS = HelloWorld.o
all: build install
build: $(APP)
$(APP): $(APP_OBJS)
$(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS)
clean:
-rm -f $(APP) *.elf *.gdb *.o
.PHONY: install image
install: $(APP)
$(TARGETINST) -d $(APP) /home/httpd/cgi-bin/$(APP)
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
Build and boot PetaLinux
Follow the instructions in part 29 to add a new appliction, build and boot PetaLinux.
Start the web server
Login to PetaLinux and start httpd (Hypertext Transfer Protocol Daemon) using port 8080:
->httpd -p 8080 -h /home/httpd
Displaying the web page
Open a web browser on the host machine and enter the following web address: 192.168.33.3:8080/cgi-bin/HelloWorld

The CGI script will execute on the server side and display the text: Hello world.
Modifying the HelloWorld program
Writing C programs normally means we have to do a number of iterations including bug fixing and adding more code, which means we have to go through this process every time.
- Change the source code
- Rebuild the Linux kernel
- Load and boot the kernel
This will take several minutes to be completed and we will spend a lot of time waiting for things to finish. Let's find a faster way to modify and test a C program. Here is the modification:
printf("Hello world from Zoocad Consulting ");
Build the the application
To find out what other possibilities we have to build our application we use the command:
->petalinux-build --help

We will use this command to build only the HelloWorld application:
->petalinux-build -c rootfs/HelloWorld -x build

The result from the build is stored in the directory: build/linux/rootfs/apps/HelloWorld

Copy the HelloWorld application
As we have the Ubuntu file system NFS mounted on our host we can copy the executable file HelloWorld to PetaLinux using the curl command (see part 34).
-> cd ..../build/linux/rootfs/apps/HelloWorld
-> curl -T HelloWorld -u root:root ftp://192.168.33.3
On the PetaLinux side:
->cd /var/ftp
->chmod 755 HelloWorld
->cp HelloWorld /home/httpd/cgi-bin/.

Top Previous Next