1

my rootfs.cpio has only following files:

[root@localhost extract]# ls 
dev  init  tmp

dev has console only.

init is cross-compiled from the program given at the end:

Then I make a my image and run linux. It runs fine but when init comes it shows error similar to the following :

Failed to open /sys/class/gpio/gpio251/direction  
Failed to open /sys/class/gpio/gpio251/value

So, I manually created these folders and files and now it looks like this:

[root@localhost extract]# ls 
    dev  init  tmp sys

inside sys I created required folders and files(empty).

But even then code is not executing and kernel panics.

Background

This code I took from a complete file system, which had all the directories expected in a linux system. I took this code cross compiled separately, and renamed it to init.

And expecting to work(Light an LED eg).

Another approach

bash> echo 240 > /sys/class/gpio/export
bash> echo out > /sys/class/gpio/gpio240/direction
bash> echo 1 > /sys/class/gpio/gpio240/value

This approach is described GPIO DRIVER. So after manually creating these required files and I cross-compiled it and renamed it to init. And then made rootfs.cpio and created my OS image. But this also did not work.

Questions Why is the code not executing properly in my own file system (partial)?

Does the code depends upon some other files or dynamic libraries (which are present in complete file system)? Why is my manually created files not working?

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <signal.h>
    int main( )
    {
        extern char *optarg;
        char *cptr;
        int gpio_value = 0;
        int nchannel = 0;

        int c;
        int i;

        opterr = 0;

int argc=5;
char *argv;
char *argv2[] = {"gpio-demo", "-g", "255", "o", "0"}; argv = argv2; 
        while ((c = getopt(argc, argv, "g:io:ck")) != -1) {
            switch (c) {
                case 'g':
                    gl_gpio_base = (int)strtoul(optarg, &cptr, 0);
                    if (cptr == optarg)
                        usage(argv[0]);
                    break;
                case 'i':
                    gpio_opt = IN;
                    break;
                case 'o':
                    gpio_opt = OUT;
                    gpio_value = (int)strtoul(optarg, &cptr, 0);
                    if (cptr == optarg)
                        usage(argv[0]);
                    break;
                case 'c':
                    gpio_opt = CYLON;
                    break;
                case 'k':
                    gpio_opt = KIT;
                    break;
                case '?':
                    usage(argv[0]);
                default:
                    usage(argv[0]);

            }
        }

        if (gl_gpio_base == 0) {
            usage(argv[0]);
        }

        nchannel = open_gpio_channel(gl_gpio_base);
        signal(SIGTERM, signal_handler); /* catch kill signal */
        signal(SIGHUP, signal_handler); /* catch hang up signal */
        signal(SIGQUIT, signal_handler); /* catch quit signal */
        signal(SIGINT, signal_handler); /* catch a CTRL-c signal */
        switch (gpio_opt) {
            case IN:
                set_gpio_direction(gl_gpio_base, nchannel, "in");
                gpio_value=get_gpio_value(gl_gpio_base, nchannel);
                fprintf(stdout,"0x%08X\n", gpio_value);
                break;
            case OUT:
                set_gpio_direction(gl_gpio_base, nchannel, "out");
                set_gpio_value(gl_gpio_base, nchannel, gpio_value);
                break;
            case CYLON:
    #define CYLON_DELAY_USECS (10000)
                set_gpio_direction(gl_gpio_base, nchannel, "out");
                for (;;) {
                    for(i=0; i < ARRAY_SIZE(cylon); i++) {
                        gpio_value=(int)cylon[i];
                        set_gpio_value(gl_gpio_base, nchannel, gpio_value);
                    }
                    usleep(CYLON_DELAY_USECS);
                }
            case KIT:
    #define KIT_DELAY_USECS (10000)
                set_gpio_direction(gl_gpio_base, nchannel, "out");
                for (;;) {
                    for (i=0; i<ARRAY_SIZE(kit); i++) {
                        gpio_value=(int)kit[i];
                        set_gpio_value(gl_gpio_base, nchannel, gpio_value);
                    }
                    usleep(KIT_DELAY_USECS);
                }
            default:
                break;
        }
        close_gpio_channel(gl_gpio_base);
        return 0;
    }
2

/sys is a special filesystem. You can't just create it and put files in it. It's like /proc, a fake filesystem provided by the kernel.

Geting /sys working requires 2 things:

  1. In your kernel configuration, you need to have CONFIG_SYSFS=y.
  2. You need to mount it with mount -t sysfs none /sys (assuming you're running from an initramfs since you mention cpio).

So the directory itself should be there so you can mount on top of it, but that's it, nothing inside it.

  • But how those files come in the directory? – user2799508 Apr 20 '14 at 9:40
  • The kernel puts them there. – Patrick Apr 20 '14 at 21:03
  • To put these files,does the kernel need additional directories such as /dev,/bin,/lib ,/home etc ? I only have in my folder /sys,/tmp ,/dev , and I am ready to put other empty directories. – user2799508 Apr 21 '14 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.