1/*
2 * Copyright (c) 2003-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  extractBootBlocks.c
25 *  bless/extractBootBlocks - Tool for extracting 'boot' 1 from System file
26 *
27 *  Created by Shantonu Sen <ssen@apple.com> on Mon May 19 2003.
28 *  Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: extractBootBlocks.c,v 1.7 2005/02/03 00:42:22 ssen Exp $
31 *
32 */
33
34#include <CoreServices/CoreServices.h>
35
36#include <libc.h>
37#include <err.h>
38
39int main(int argc, char *argv[]) {
40
41    char *system = NULL;
42    FSRef ref;
43    OSStatus ret;
44    short resFile;
45    Handle bbHandle;
46    Size size;
47    size_t outputsize;
48
49    if(argc != 2) {
50	fprintf(stderr, "Usage: %s /S/L/CS/System\n", argv[0]);
51	exit(1);
52    }
53
54    system = argv[1];
55
56    if(isatty(fileno(stdout))) {
57	fprintf(stderr, "I refuse to spew to a tty\n");
58	exit(1);
59    }
60
61    ret = FSPathMakeRef((UInt8*)system, &ref, NULL);
62    if(ret != noErr) {
63	err(1, "Something bad happened with FSPathMakeRef");
64    }
65
66    resFile = FSOpenResFile(&ref, fsRdPerm);
67
68    bbHandle = GetResource('boot', 1);
69
70    if(!bbHandle || !*bbHandle) {
71	CloseResFile(resFile);
72	err(1, "Could not get 'boot' 1 resource");
73    }
74
75    DetachResource(bbHandle);
76
77    size = GetHandleSize(bbHandle);
78    outputsize = write(fileno(stdout), *bbHandle, size);
79
80    if(outputsize != size) {
81	DisposeHandle(bbHandle);
82	CloseResFile(resFile);
83	err(1, "Could not write all of boot blocks to stdout");
84    }
85
86    DisposeHandle(bbHandle);
87
88    CloseResFile(resFile);
89}
90