EMILE/libemile/emile_second_set_configuration.c

59 lines
1.2 KiB
C
Raw Normal View History

2005-11-27 23:45:18 +00:00
/*
*
* (c) 2005-2007 Laurent Vivier <Laurent@Vivier.EU>
2005-11-27 23:45:18 +00:00
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "libemile.h"
#include "emile.h"
#include "bootblock.h"
2007-09-08 23:09:14 +00:00
int emile_second_set_configuration(int fd, int8_t *configuration)
2005-11-27 23:45:18 +00:00
{
emile_l2_header_t header;
int ret;
int size;
int len;
char *buf;
2005-11-27 23:45:18 +00:00
if (configuration == NULL)
return EEMILE_CANNOT_READ_SECOND;
2007-09-08 23:09:14 +00:00
len = strlen ((char*)configuration) + 1; /* + 1 for ending 0 */
2005-11-27 23:45:18 +00:00
2007-08-24 09:27:29 +00:00
memset(&header, 0, sizeof(header));
2005-11-27 23:45:18 +00:00
ret = read(fd, &header, sizeof(header));
2007-08-24 09:27:29 +00:00
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
{
lseek(fd, FIRST_LEVEL_SIZE, SEEK_SET);
2007-08-24 09:27:29 +00:00
ret = read(fd, &header, sizeof(header));
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
return EEMILE_INVALID_SECOND;
}
2005-11-27 23:45:18 +00:00
if (ret != sizeof(header))
return EEMILE_CANNOT_READ_SECOND;
size = read_short(&header.conf_size);
if (len > size)
2005-11-28 20:54:29 +00:00
return EEMILE_INVALID_SECOND;
2005-11-27 23:45:18 +00:00
buf = malloc(size);
memset(buf, 0, size);
memcpy(buf, configuration, len);
ret = write(fd, buf, size);
2008-07-29 00:28:35 +00:00
if (ret != size)
2005-11-27 23:45:18 +00:00
return EEMILE_CANNOT_WRITE_SECOND;
free(buf);
2005-11-27 23:45:18 +00:00
return 0;
}