1 /* ----------------------------------------------------------------------- *
3 * Copyright 2010 Intel Corp. - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * common functions for extlinux & syslinux installer
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/mount.h>
34 #include <linux/fs.h> /* FIGETBSZ, FIBMAP */
35 #include <linux/msdos_fs.h> /* FAT_IOCTL_SET_ATTRIBUTES */
43 # define dprintf printf
45 # define dprintf(...) ((void)0)
48 #define SECTOR_SHIFT 9
49 #define EXT2_IMMUTABLE_FL 0x00000010 /* Immutable file */
54 #define EXT2_IOC_GETFLAGS _IOR('f', 1, long)
55 #define EXT2_IOC_SETFLAGS _IOW('f', 2, long)
56 #define EXT2_IOC_GETVERSION _IOR('v', 1, long)
57 #define EXT2_IOC_SETVERSION _IOW('v', 2, long)
59 static void die(const char *msg)
66 * read/write wrapper functions
68 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
70 char *bufp = (char *)buf;
75 rv = pread(fd, bufp, count, offset);
78 } else if (rv == -1) {
95 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
97 const char *bufp = (const char *)buf;
102 rv = pwrite(fd, bufp, count, offset);
105 } else if (rv == -1) {
106 if (errno == EINTR) {
109 die(strerror(errno));
123 * Set and clear file attributes
125 void clear_attributes(int fd)
129 if (!fstat(fd, &st)) {
135 if (!ioctl(fd, EXT2_IOC_GETFLAGS, &flags)) {
136 flags &= ~EXT2_IMMUTABLE_FL;
137 ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
143 uint32_t attr = 0x00; /* Clear all attributes */
144 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
150 fchmod(fd, st.st_mode | S_IWUSR);
154 void set_attributes(int fd)
158 if (!fstat(fd, &st)) {
159 fchmod(fd, st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH));
165 if (st.st_uid == 0 && !ioctl(fd, EXT2_IOC_GETFLAGS, &flags)) {
166 flags |= EXT2_IMMUTABLE_FL;
167 ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
173 uint32_t attr = 0x07; /* Hidden+System+Readonly */
174 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
186 int sectmap(int fd, uint32_t * sectors, int nsectors)
188 unsigned int blksize, blk, nblk;
192 if (ioctl(fd, FIGETBSZ, &blksize))
195 /* Number of sectors per block */
196 blksize >>= SECTOR_SHIFT;
202 dprintf("querying block %u\n", blk);
203 if (ioctl(fd, FIBMAP, &blk))
207 for (i = 0; i < blksize; i++) {
211 dprintf("Sector: %10u\n", blk);