You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
1.8 KiB
104 lines
1.8 KiB
7 years ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
#include <getopt.h>
|
||
|
|
||
|
#include <image.h>
|
||
|
#include <info.h>
|
||
|
#include <macros.h>
|
||
|
#include <sign.h>
|
||
|
|
||
|
enum {
|
||
|
OPTION_HELP = 'h',
|
||
|
OPTION_IMAGE = 'i',
|
||
|
};
|
||
|
|
||
|
struct args {
|
||
|
const char *image;
|
||
|
};
|
||
|
|
||
|
static int parse_args(struct args *args, int argc, char *argv[])
|
||
|
{
|
||
|
struct option options[] = {
|
||
|
{ "help", no_argument, NULL, OPTION_HELP },
|
||
|
{ "image", required_argument, 0, OPTION_IMAGE },
|
||
|
{ NULL, 0, 0, 0 },
|
||
|
};
|
||
|
int ret;
|
||
|
|
||
|
while ((ret = getopt_long(argc, (char * const *)argv, "hi:d:k:", options,
|
||
|
NULL)) >= 0) {
|
||
|
switch (ret) {
|
||
|
case OPTION_HELP: return -1;
|
||
|
case OPTION_IMAGE: args->image = optarg; break;
|
||
|
default: break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int do_get_offset(int argc, char *argv[])
|
||
|
{
|
||
|
struct args args;
|
||
|
struct rots_hdr hdr;
|
||
|
FILE *fp;
|
||
|
|
||
|
if (parse_args(&args, argc, argv) < 0) {
|
||
|
fprintf(stderr, "invalid\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (!(fp = fopen(args.image, "rb"))) {
|
||
|
fprintf(stderr, "error: file '%s' not found.\n", args.image);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (rots_read_hdr(fp, &hdr) < 0) {
|
||
|
fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image);
|
||
|
goto err_close_fp;
|
||
|
}
|
||
|
|
||
|
printf("%zu\n", ftell(fp));
|
||
|
fclose(fp);
|
||
|
|
||
|
return 0;
|
||
|
|
||
|
err_close_fp:
|
||
|
fclose(fp);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int do_get_size(int argc, char *argv[])
|
||
|
{
|
||
|
struct args args;
|
||
|
struct rots_hdr hdr;
|
||
|
FILE *fp;
|
||
|
|
||
|
if (parse_args(&args, argc, argv) < 0) {
|
||
|
fprintf(stderr, "invalid\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (!(fp = fopen(args.image, "rb"))) {
|
||
|
fprintf(stderr, "error: file '%s' not found.\n", args.image);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (rots_read_hdr(fp, &hdr) < 0) {
|
||
|
fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image);
|
||
|
goto err_close_fp;
|
||
|
}
|
||
|
|
||
|
printf("%zu\n", hdr.size);
|
||
|
fclose(fp);
|
||
|
|
||
|
return 0;
|
||
|
|
||
|
err_close_fp:
|
||
|
fclose(fp);
|
||
|
return -1;
|
||
|
}
|