zboot/piggyback.c

/*
 *    linux/zBoot/piggyback.c
 *
 *    (C) 1993 Hannu Savolainen
 */

/*
 *    This program reads the compressed system image from stdin and
 *    encapsulates it into an object file written to the stdout.
 */
//这个程序读取压缩的system映像文件从标准输入然后压缩后写入标准输出
#include <stdio.h>
#include <unistd.h>
#include <a.out.h>

//主函数
int main(int argc, char *argv[])
{
    int c, n=0, len=0;
    //定义缓冲区
    char tmp_buf[512*1024];
    
    //可执行文件头
    struct exec obj = {0x00640107};    /* object header */
    //输入的数据和输入的长度
    char string_names[] = {"_input_data_input_len"};

    //标号表,变量名
    struct nlist var_names[2] = /* Symbol table */
        {    
            {    /* _input_data    */
                (char *)4, 7, 0, 0, 0
            },
            {    /* _input_len */
                (char *)16, 7, 0, 0, 0
            }
        };


    len = 0;
    //从标准输入中读取数据到缓冲区
    while ((n = read(0, &tmp_buf[len], sizeof(tmp_buf)-len+1)) > 0)
          len += n;

    //校验读取的长度
    if (n==-1)
    {
        perror("stdin");
        exit(-1);
    }

    if (len >= sizeof(tmp_buf))
    {
        fprintf(stderr, "%s: Input too large ", argv[0]);
        exit(-1);
    }

    //输出长度信息
    fprintf(stderr, "Compressed size %d. ", len);

/*
 *    Output object header
 */
    //输出对象头
    obj.a_data = len + sizeof(long);
    obj.a_syms = sizeof(var_names);
    write(1, (char *)&obj, sizeof(obj));

/*
 *    Output data segment (compressed system & len)
 */
    //输出数据段
    write(1, tmp_buf, len);
    write(1, (char *)&len, sizeof(len));

/*
 *    Output symbol table
 */
    //输出符号表
    var_names[1].n_value = len;
    write(1, (char *)&var_names, sizeof(var_names));

/*
 *    Output string table
 */
    //输出字符表
    len = sizeof(string_names) + sizeof(len);
    write(1, (char *)&len, sizeof(len));
    write(1, string_names, sizeof(string_names));

    exit(0);

}

原文地址:https://www.cnblogs.com/xiaofengwei/p/3753351.html