4 * Copyright (c) 2009 NONAKA Kimihiro <nonaka@netbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/bootblock.h>
35 #include "pathnames.h"
37 #include <lib/libsa/loadfile.h>
39 #include "compat_linux.h"
41 static int fdloadfile_zboot(int fd
, u_long
*marks
, int flags
);
42 static int zboot_exec(int fd
, u_long
*marks
, int flags
);
45 loadfile_zboot(const char *fname
, u_long
*marks
, int flags
)
50 if ((fd
= open(fname
, 0)) < 0) {
51 WARN(("open %s", fname
? fname
: "<default>"));
55 /* Load it; save the value of errno across the close() call */
56 if ((error
= fdloadfile_zboot(fd
, marks
, flags
)) != 0) {
66 fdloadfile_zboot(int fd
, u_long
*marks
, int flags
)
72 /* Read the exec header. */
73 if (lseek(fd
, 0, SEEK_SET
) == (off_t
)-1)
75 nr
= read(fd
, &elf32
, sizeof(elf32
));
77 WARN(("read header failed"));
80 if (nr
!= sizeof(elf32
)) {
81 WARN(("read header short"));
86 if (memcmp(elf32
.e_ident
, ELFMAG
, SELFMAG
) == 0 &&
87 elf32
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
88 rval
= zboot_exec(fd
, marks
, flags
);
101 zboot_exec(int fd
, u_long
*marks
, int flags
)
103 static char bibuf
[BOOTINFO_MAXSIZE
];
105 struct btinfo_common
*help
;
115 memcpy(p
, &bootinfo
->nentries
, sizeof(bootinfo
->nentries
));
116 p
+= sizeof(bootinfo
->nentries
);
117 for (i
= 0; i
< bootinfo
->nentries
; i
++) {
118 help
= (struct btinfo_common
*)(bootinfo
->entry
[i
]);
119 if ((p
- bibuf
) + help
->len
> BOOTINFO_MAXSIZE
)
122 memcpy(p
, help
, help
->len
);
126 tofd
= uopen(_PATH_ZBOOT
, LINUX_O_WRONLY
);
128 printf("%s: can't open (errno %d)\n", _PATH_ZBOOT
, errno
);
132 if (uwrite(tofd
, bibuf
, p
- bibuf
) != p
- bibuf
)
133 printf("setbootargs: argument write error\n");
135 /* Commit boot arguments. */
141 tofd
= uopen(_PATH_ZBOOT
, LINUX_O_WRONLY
);
143 printf("%s: can't open (errno %d)\n", _PATH_ZBOOT
, errno
);
147 if (lseek(fd
, 0, SEEK_SET
) != 0) {
148 printf("%s: seek error\n", _PATH_ZBOOT
);
152 while ((sz
= read(fd
, buf
, sizeof(buf
))) == sizeof(buf
)) {
153 if ((sz
= uwrite(tofd
, buf
, sz
)) != sizeof(buf
)) {
154 printf("%s: write error\n", _PATH_ZBOOT
);
160 printf("zboot_exec: read error\n");
164 if (sz
>= 0 && uwrite(tofd
, buf
, sz
) != sz
) {
165 printf("zboot_exec: write error\n");