]> git.saurik.com Git - apple/system_cmds.git/blob - kpgo.tproj/kpgo.c
system_cmds-671.10.3.tar.gz
[apple/system_cmds.git] / kpgo.tproj / kpgo.c
1 /*
2 * Copyright (c) 2014 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <sys/pgo.h>
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 static void usage(char **argv)
35 {
36 fprintf (stderr, "usage: %s [-H] [-m] [-w] [uuid] >datafile\n", argv[0]);
37 fprintf (stderr, " uuid : the UUID of a kext\n");
38 fprintf (stderr, " -H : grab data for the HIB segment\n");
39 fprintf (stderr, " -w : wait for the kext to be unloaded\n");
40 fprintf (stderr, " -m : request metadata\n");
41 exit(1);
42 }
43
44 int main(int argc, char **argv)
45 {
46 int flags = 0;
47 int data_flags = 0;
48 uuid_t *uuidp = NULL;
49 uuid_t uuid;
50 int c;
51
52 while ((c = getopt(argc, argv, "hHwm")) != EOF) {
53 switch(c) {
54 case 'H':
55 flags |= PGO_HIB;
56 break;
57 case 'm':
58 flags |= PGO_METADATA;
59 break;
60 case 'w':
61 data_flags |= PGO_WAIT_FOR_UNLOAD;
62 break;
63 case '?':
64 case 'h':
65 default:
66 usage(argv);
67 break;
68 }
69 }
70
71 if (optind < argc)
72 {
73 if (optind == argc - 1 &&
74 0 == uuid_parse(argv[optind], uuid))
75 {
76 uuidp = &uuid;
77 } else {
78 usage(argv);
79 }
80 }
81
82 ssize_t size = grab_pgo_data(uuidp, flags, NULL, 0);
83
84 if (size < 0)
85 {
86 perror("grab_pgo_data");
87 return 1;
88 }
89
90
91 fprintf (stderr, "size = %ld\n", (long) size);
92
93 unsigned char *buffer = valloc(size);
94 if (!buffer)
95 {
96 perror("valloc");
97 return 1;
98 }
99
100 ssize_t r = grab_pgo_data(uuidp, flags | data_flags, buffer, size);
101
102
103 if (r < 0)
104 {
105 perror("grab_pgo_data");
106 return 1;
107 }
108
109 if (isatty(STDOUT_FILENO)) {
110 fprintf (stderr, "%s: refusing to write binary data to a tty!\n", argv[0]);
111 return 1;
112 }
113
114 while (size > 0) {
115 errno = 0;
116 r = write(STDOUT_FILENO, buffer, size);
117 if (r > 0) {
118 buffer += r;
119 size -= r;
120 } else {
121 perror ("write");
122 return 1;
123 }
124 }
125
126 return 0;
127
128 }