1 /* envblk.c - Common function for environment block. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/types.h>
22 #include <grub/misc.h>
23 #include <grub/lib/envblk.h>
26 grub_envblk_find (char *buf
)
31 pd
= (grub_uint32_t
*) buf
;
33 for (len
= GRUB_ENVBLK_MAXLEN
- 6; len
> 0; len
-= 4, pd
++)
34 if (*pd
== GRUB_ENVBLK_SIGNATURE
)
38 p
= (grub_envblk_t
) pd
;
47 grub_envblk_insert (grub_envblk_t envblk
, char *name
, char *value
)
53 nl
= grub_strlen (name
);
55 pend
= p
+ envblk
->length
;
59 if ((! found
) && (! grub_memcmp (name
, p
, nl
)) && (p
[nl
] == '='))
62 p
+= grub_strlen (p
) + 1;
71 len1
= grub_strlen (found
);
72 len2
= grub_strlen (value
);
73 if ((p
- envblk
->data
) + 1 - len1
+ len2
> envblk
->length
)
76 grub_memcpy (found
+ len2
+ 1, found
+ len1
+ 1, (p
- found
) - len1
);
77 grub_strcpy (found
, value
);
81 int len2
= grub_strlen (value
);
83 if ((p
- envblk
->data
) + nl
+ 1 + len2
+ 2 > envblk
->length
)
86 grub_strcpy (p
, name
);
88 grub_strcpy (p
+ nl
+ 1, value
);
89 p
[nl
+ 1 + len2
+ 1] = 0;
96 grub_envblk_delete (grub_envblk_t envblk
, char *name
)
102 nl
= grub_strlen (name
);
104 pend
= p
+ envblk
->length
;
108 if ((! found
) && (! grub_memcmp (name
, p
, nl
)) && (p
[nl
] == '='))
111 p
+= grub_strlen (p
) + 1;
120 len
= grub_strlen (found
);
121 grub_memcpy (found
, found
+ len
+ 1, (p
- found
) - len
);
126 grub_envblk_iterate (grub_envblk_t envblk
,
127 int hook (char *name
, char *value
))
132 pend
= p
+ envblk
->length
;
139 v
= grub_strchr (p
, '=');
152 p
+= grub_strlen (p
) + 1;